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
stk_arch_arm-cortex-m.cpp
Go to the documentation of this file.
1/*
2 * SuperTinyKernel(TM) RTOS: Lightweight High-Performance Deterministic C++ RTOS for Embedded Systems.
3 *
4 * Source: https://github.com/SuperTinyKernel-RTOS
5 *
6 * Copyright (c) 2022-2026 Neutron Code Limited <stk@neutroncode.com>. All Rights Reserved.
7 * License: MIT License, see LICENSE for a full text.
8 */
9
10// note: If missing, this header must be customized (get it in the root of the source folder) and
11// copied to the /include folder manually.
12#include "stk_config.h"
13
14#ifdef _STK_ARCH_ARM_CORTEX_M
15
16#ifdef _STK_CORTEX_M_TRUSTZONE
17#include <arm_cmse.h>
18#endif
19
20#ifdef __ICCARM__
21 #include <xatomic.h>
22#endif
23
24#include "stk.h"
25#include "stk_arch.h"
28
29using namespace stk;
30
31// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32#if !defined(_STK_CORTEX_M_TRUSTZONE_NON_SECURE)
33// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34
36#ifndef __CORTEX_M
37#error Expecting __CORTEX_M with value corresponding to Cortex-M model (0, 3, 4, 7, 33, ...)!
38#endif
39
41#if !defined(SysTick)
42 #error "SysTick peripheral definition is missing!"
43#endif
44
46#if (__CORTEX_M > 1U) && STK_TICKLESS_USE_ARM_DWT && !defined(DWT)
47 #error "DWT peripheral definition is missing!"
48#endif
49
51#define STK_CORTEX_M_FPU ((__FPU_PRESENT == 1U) && (__FPU_USED == 1U))
52
54#ifdef __MPU_PRESENT
55 #define STK_CORTEX_M_MPU (__MPU_PRESENT == 1U)
56#else
57 #define STK_CORTEX_M_MPU (0)
58#endif
59#if !STK_CORTEX_M_MPU
60 #if STK_MPU
61 #error "MPU is not present on this platform, STK_MPU feature is not supported!"
62 #endif
63 #if STK_MPU_STACK_GUARD
64 #error "MPU is not present on this platform, STK_MPU_STACK_GUARD feature is not supported!"
65 #endif
66#endif
67#if STK_MPU_STACK_GUARD && !STK_MPU
68 #error "Enable MPU support (STK_MPU=1) to use per-task MPU feature (STK_MPU_STACK_GUARD=1)!"
69#endif
70
83#ifndef STK_CORTEX_M_MPU_TASK_REGION_IDX
84 #define STK_CORTEX_M_MPU_TASK_REGION_IDX (STK_CORTEX_M_MPU_REGIONS_MAX - STK_MPU_TASK_REGIONS)
85#endif
86
99#ifndef STK_CORTEX_M_MPU_TASK_REGION_IDX_NS
100 #define STK_CORTEX_M_MPU_TASK_REGION_IDX_NS (STK_CORTEX_M_MPU_REGIONS_MAX - STK_MPU_TASK_REGIONS_NS)
101#endif
102
104#define STK_CORTEX_M_MANAGE_LR (__CORTEX_M >= 3U)
105
107#define STK_CORTEX_M_PAC (__ARM_FEATURE_PA_BITS && (__CORTEX_M >= 85U))
108
110#define STK_CORTEX_M_EXC_RETURN_THREAD_PSP (0xFFFFFFFDU) // Thread mode, PSP, basic
111
112// ISR priorities:
113#define STK_CORTEX_M_ISR_PRIORITY_HIGHEST (0U)
114#define STK_CORTEX_M_ISR_PRIORITY_LOWEST (0xFFU)
115
116#ifdef CONTROL_nPRIV_Msk
119#define STK_CORTEX_M_BASEPRI_CS \
120 (static_cast<uint32_t>(STK_CORTEX_M_SVCALL_ISR_PRIORITY + 1U) << (8U - __NVIC_PRIO_BITS))
121#endif
122
124#if STK_CORTEX_M_MANAGE_LR
125 #define STK_CORTEX_M_REGISTER_COUNT (17U)
126#else
127 #define STK_CORTEX_M_REGISTER_COUNT (16U)
128#endif
129
139#ifdef _STK_CORTEX_M_TRUSTZONE
140 #define STK_CORTEX_M_TZ_REGISTER_COUNT (4U)
141#else
142 #define STK_CORTEX_M_TZ_REGISTER_COUNT (0U)
143#endif
144
153#ifdef CONTROL_nPRIV_Msk
154 #define STK_CORTEX_M_PRIVILEGE_FRAME (1)
155#else
156 #define STK_CORTEX_M_PRIVILEGE_FRAME (0)
157#endif
158
160#if STK_CORTEX_M_PRIVILEGE_FRAME
161 #define STK_CORTEX_M_PRIV_REGISTER_COUNT (1U)
162#else
163 #define STK_CORTEX_M_PRIV_REGISTER_COUNT (0U)
164#endif
165
167#define STK_CORTEX_M_TOTAL_REGISTER_COUNT \
168 (STK_CORTEX_M_REGISTER_COUNT + STK_CORTEX_M_TZ_REGISTER_COUNT + STK_CORTEX_M_PRIV_REGISTER_COUNT)
169
171#ifndef STK_SYSTICK_HANDLER
172 #define STK_SYSTICK_HANDLER SysTick_Handler
173#endif
174
176#ifndef STK_PENDSV_HANDLER
177 #define STK_PENDSV_HANDLER PendSV_Handler
178#endif
179
181#ifndef STK_SVC_HANDLER
182 #define STK_SVC_HANDLER SVC_Handler
183#endif
184
186#ifndef STK_USE_MEMMANAGE_HANDLER
187#define STK_USE_MEMMANAGE_HANDLER (0)
188#endif
189#if STK_USE_MEMMANAGE_HANDLER
190 #ifndef STK_MEMMANAGE_HANDLER
191 #define STK_MEMMANAGE_HANDLER MemManage_Handler
192 #endif
193#endif
194
196#ifndef STK_USE_HARDFAULT_HANDLER
197#define STK_USE_HARDFAULT_HANDLER (0)
198#endif
199#if STK_USE_HARDFAULT_HANDLER
200 #ifndef STK_HARDFAULT_HANDLER
201 #define STK_HARDFAULT_HANDLER HardFault_Handler
202 #endif
203#endif
204
212#ifndef STK_SPINLOCK_TIMEOUT_US
213 #define STK_SPINLOCK_TIMEOUT_US (5LL * 1000LL * 1000LL) // 5 sec
214#endif
215
225#ifndef STK_SPINLOCK_MIN_CYCLES_PER_ITER
226 #define STK_SPINLOCK_MIN_CYCLES_PER_ITER (4U)
227#endif
228
235enum ESvcCommandId : uint8_t
236{
237 SVC_START_SCHEDULING = 0U,
238 SVC_ENTER_CRITICAL,
239 SVC_EXIT_CRITICAL,
240 SVC_FORCE_SWITCH
241#if STK_MPU
242 , SVC_BOOST_PRIV
243#endif
244};
245
246// Shortcuts:
247#define STK_ASM_EXIT_FROM_HANDLER "BX LR" // use in naked exception/ISR handlers
248#define STK_ASM_DISABLE_INTERRUPTS "CPSID i"
249#define STK_ASM_ENABLE_INTERRUPTS "CPSIE i"
250
251// Wrapper to survive LTO optimization.
252#if STK_SEGGER_SYSVIEW
253extern "C" __stk_attr_used void StkSystemView_RecordEnterISR()
254{
255 SEGGER_SYSVIEW_RecordEnterISR();
256}
257#endif
258
259// ----------------------------------------------------------------------------
261namespace {
262namespace hw_int {
263namespace reg {
264// ----------------------------------------------------------------------------
265
267namespace PC {
268
270enum ERegMask : Word
271{
272 MASK_THUMB_BIT = (1U << 0U)
273};
274
276__stk_forceinline Word ClearThumbBit(Word REG_PC) noexcept
277{
278 return (REG_PC & ~static_cast<Word>(ERegMask::MASK_THUMB_BIT));
279}
280
281} // namespace PC
282
284namespace XPSR {
285
287constexpr Word DEFAULT_INIT = 0U;
288
290enum ERegMask : Word
291{
292 MASK_T_BIT = (1U << 24U)
293};
294
296__stk_forceinline Word SetThumbExecution(Word REG_XPSR) noexcept
297{
298 return (REG_XPSR | static_cast<Word>(ERegMask::MASK_T_BIT));
299}
300
301} // namespace XPSR
302
304namespace CONTROL {
305
307constexpr Word DEFAULT_INIT = 0U;
308
310enum ERegMask : Word
311{
312 MASK_nPRIV = (1U << 0),
313 MASK_SPSEL = (1U << 1),
314 MASK_FPCA = (1U << 2),
315 MASK_SFPA = (1U << 3)
316};
317
319__stk_attr_unused inline Word SetUnprivileged(Word REG_CONTROL) noexcept
320{
321 return (REG_CONTROL | static_cast<Word>(ERegMask::MASK_nPRIV));
322}
323
325__stk_attr_unused inline Word SetPrivileged(Word REG_CONTROL) noexcept
326{
327 return (REG_CONTROL & ~static_cast<Word>(ERegMask::MASK_nPRIV));
328}
329
331__stk_attr_unused inline Word SetSPSelectionToPSP(Word REG_CONTROL) noexcept
332{
333 return (REG_CONTROL | static_cast<Word>(ERegMask::MASK_SPSEL));
334}
335
336} // namespace CONTROL
337
338// ----------------------------------------------------------------------------
339} // namespace reg
340} // namespace hw_int
341} // namespace
342// ----------------------------------------------------------------------------
343
344// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
345#if STK_MPU
346// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
347
348// ----------------------------------------------------------------------------
349namespace {
350namespace hw_int {
351namespace pmsa {
352// ----------------------------------------------------------------------------
353
354// Helpers to extract effective physical bounds [start, end_inclusive]
355struct RegionBounds
356{
357 Word start;
358 Word end;
359 bool valid;
360};
361
362__stk_forceinline RegionBounds GetRegionBounds(const MpuRegionConfig &cfg)
363{
364 RegionBounds result = { 0U, 0U, false };
365
366 if ((cfg.size != 0U) && (cfg.access_perm != hw::mpu::EMpuAccess::ACCESS_NONE))
367 {
368 #if STK_ARCH_ARMV8_M
369 // PMSAv8: 32-byte aligned base and size
370 result.start = cfg.addr & ~31U;
371 result.end = result.start + cfg.size - 1U;
372 #else
373 // PMSAv7: naturally aligned power-of-two size
374 result.start = cfg.addr & ~(cfg.size - 1U);
375 result.end = result.start + cfg.size - 1U;
376 #endif
377
378 result.valid = (result.start < result.end);
379 }
380
381 return result;
382}
383
384__stk_forceinline bool IsOverlapping(const RegionBounds &a, const RegionBounds &b)
385{
386#if STK_ARCH_ARMV8_M
387 // standard closed-interval collision test [start, end]
388 return ((!a.valid || !b.valid) ? false : (a.start <= b.end) && (b.start <= a.end));
389#else
390 // ARMv7-M (PMSAv7) supports regions overlapping, so we always allow overlapping
391 STK_UNUSED(a);
392 STK_UNUSED(b);
393 return false;
394#endif
395}
396
397void ValidateNoOverlaps(const MpuRegionConfig dynamic_cfg_list[], size_t dynamic_cfg_count,
398 const MpuRegionConfig static_cfg_list[], size_t static_cfg_count)
399{
400 // 1. Validate internal overlaps within dynamic list
401 for (size_t i = 0U; i < dynamic_cfg_count; ++i)
402 {
403 const RegionBounds bounds_i = GetRegionBounds(dynamic_cfg_list[i]);
404 if (!bounds_i.valid)
405 {
406 continue;
407 }
408
409 for (size_t j = i + 1U; j < dynamic_cfg_count; ++j)
410 {
411 const RegionBounds bounds_j = GetRegionBounds(dynamic_cfg_list[j]);
412 if (!bounds_j.valid)
413 {
414 continue;
415 }
416
417 // Assert failure means two active dynamic configurations cross boundaries
418 if (IsOverlapping(bounds_i, bounds_j))
419 {
420 STK_KERNEL_PANIC(KERNEL_PANIC_BAD_MEMORY_REGION);
421 }
422 }
423
424 // 2. Validate dynamic region 'i' against static regions from overrider
425 if (static_cfg_list != nullptr)
426 {
427 for (size_t k = 0U; k < static_cfg_count; ++k)
428 {
429 const RegionBounds static_bounds = GetRegionBounds(static_cfg_list[k]);
430 if (!static_bounds.valid)
431 {
432 continue;
433 }
434
435 // Assert failure means a dynamic region collides with a static region
436 if (IsOverlapping(bounds_i, static_bounds))
437 {
438 STK_KERNEL_PANIC(KERNEL_PANIC_BAD_MEMORY_REGION);
439 }
440 }
441 }
442 }
443}
444
445// ----------------------------------------------------------------------------
446} // namespace pmsa
447} // namespace hw_int
448} // namespace
449// ----------------------------------------------------------------------------
450
451// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
452#endif
453// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
454
455// ----------------------------------------------------------------------------
456namespace {
457// ----------------------------------------------------------------------------
458
459#if defined(_STK_CORTEX_M_TRUSTZONE) && (STK_CORTEX_M_TZ_REGISTER_COUNT != 0U)
481struct TrustZoneFrame
482{
483 Word PSP_NS;
484 Word CONTROL_NS;
485 Word PSPLIM;
486 Word PSPLIM_NS;
487};
488#define STK_CORTEX_M_TRUSTZONE_FRAME (1)
489#else
490#define STK_CORTEX_M_TRUSTZONE_FRAME (0)
491#endif
492
493#if STK_CORTEX_M_PRIVILEGE_FRAME
501struct PrivilegeFrame
502{
503 Word CONTROL;
504};
505#endif // STK_CORTEX_M_PRIVILEGE_FRAME
506
518struct TaskFrame
519{
520#if STK_CORTEX_M_MANAGE_LR
521 Word EXC_RETURN;
522#endif
523 hw::ExceptionFrame exc;
524};
525
526// ----------------------------------------------------------------------------
527} // namespace
528// ----------------------------------------------------------------------------
529
530// Local functions of the driver:
531// ----------------------------------------------------------------------------
532namespace {
533namespace drv {
534// ----------------------------------------------------------------------------
535
536void OnTaskRun(ITask *runnable);
537void OnTaskExit();
538void OnSchedulerSleep();
539void OnSchedulerSleepOverride();
540void OnSchedulerExit();
541#if STK_SEGGER_SYSVIEW
542void SendSysDesc()
543{
544 SEGGER_SYSVIEW_SendSysDesc(STK_PACKAGE_NAME);
545}
546#endif
547
548// ----------------------------------------------------------------------------
549} // namespace drv
550} // namespace
551// ----------------------------------------------------------------------------
552
558extern "C" volatile EKernelPanicId g_StkLastPanicId;
561volatile EKernelPanicId g_StkLastPanicId = KERNEL_PANIC_NONE;
562
563// ----------------------------------------------------------------------------
564namespace {
565// ----------------------------------------------------------------------------
566
567// Local static variables:
569#if STK_CORE_FREQ_UNIFIED
570uint32_t s_StkSystemCoreClock[1U];
571#else
572uint32_t s_StkSystemCoreClock[STK_ARCH_CPU_COUNT];
573#endif
574
584uint32_t s_StkSpinlockTimeoutIters = 0xFFFFFFFU;
585
588volatile bool s_StkCortexmCsuLock = false;
589
593__stk_forceinline void HW_StartScheduler()
594{
595 __asm volatile("SVC %0"
596 : /* output: none */
597 : "I"(SVC_START_SCHEDULING)
598 : "memory" /* protect against compiler reordering */
599 );
600}
601
604__stk_forceinline void HW_ForceContextSwitch(TId id)
605{
606 __asm volatile(
607 "MOV r0, %0 \n" /* move id parameter into r0 */
608 "SVC %1 \n" /* trigger SVC interrupt */
609 : /* output: none */
610 : "r" (id),
611 "I" (SVC_FORCE_SWITCH) /* 'r' uses any general register, mapped to r0 in assembly */
612 : "r0", "memory" /* r0 is clobbered with id, protect against compiler reordering */
613 );
614}
615
625__stk_forceinline void HW_UnprivEnterCriticalSection()
626{
627 __asm volatile("SVC %0"
628 : /* output: none */
629 : "I"(SVC_ENTER_CRITICAL)
630 : "memory" /* protect against compiler reordering */
631 );
632}
633
643__stk_forceinline void HW_UnprivExitCriticalSection()
644{
645 __asm volatile("SVC %0"
646 : /* output: none */
647 : "I"(SVC_EXIT_CRITICAL)
648 : "memory" /* protect against compiler reordering */
649 );
650}
651
654__stk_forceinline void HW_DisableInterrupts()
655{
656#if (defined(__clang__) && defined(__ARMCOMPILER_VERSION)) || defined(__ICCARM__)
657 __asm volatile(STK_ASM_DISABLE_INTERRUPTS ::: "memory");
658#else
659 __disable_irq();
660#endif
661}
662
665__stk_forceinline void HW_EnableInterrupts()
666{
667#if (defined(__clang__) && defined(__ARMCOMPILER_VERSION)) || defined(__ICCARM__)
668 __asm volatile(STK_ASM_ENABLE_INTERRUPTS ::: "memory");
669#else
670 __enable_irq();
671#endif
672}
673
678__stk_forceinline bool HW_InterruptsDisabled()
679{
680#if (defined(__clang__) && defined(__ARMCOMPILER_VERSION)) || defined(__ICCARM__)
681 Word primask;
682 __asm volatile("MRS %0, primask" : "=r"(primask));
683 return ((primask & 1U) != 0U);
684#else
685 return ((__get_PRIMASK() & 1U) != 0U);
686#endif
687}
688
692__stk_forceinline uint32_t HW_CriticalSectionStart()
693{
694 const uint32_t ses = __get_PRIMASK();
695 HW_DisableInterrupts();
696
697 // ensure the disable is recognized before subsequent code
698 __DSB();
699 __ISB();
700
701 return ses;
702}
703
707__stk_forceinline void HW_CriticalSectionEnd(uint32_t ses)
708{
709 // ensure all memory work is finished before re-enabling
710 __DSB();
711 __set_PRIMASK(ses);
712 // synchronization point: any pending interrupt can be serviced immediately at this boundary
713 __ISB();
714}
715
718__stk_forceinline void HW_EnterSleepMode()
719{
720 SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk; // disable deep-sleep, go into a WAIT mode (sleep)
721 __DSB(); // ensure store takes effect (see ARM info)
722 __WFI();
723}
724
725// IAR wrappers for: __atomic_test_and_set, __atomic_clear.
726#ifdef __ICCARM__
727 #define __ATOMIC_RELAXED __MEMORY_ORDER_RELAXED__
728 #define __ATOMIC_CONSUME __MEMORY_ORDER_CONSUME__
729 #define __ATOMIC_ACQUIRE __MEMORY_ORDER_ACQUIRE__
730 #define __ATOMIC_RELEASE __MEMORY_ORDER_RELEASE__
731 #define __ATOMIC_ACQ_REL __MEMORY_ORDER_ACQ_REL__
732 #define __ATOMIC_SEQ_CST __MEMORY_ORDER_SEQ_CST__
733
734 __stk_forceinline bool __atomic_test_and_set(volatile bool *ptr, int memorder)
735 {
736 #ifdef __ICCARM__
737 STK_STATIC_ASSERT(sizeof(std::__iar_atomic_flag) == sizeof(bool));
738 #endif
739
740 return std::__iar_atomic_flag_test_and_set(ptr, memorder);
741 }
742
743 __stk_forceinline void __atomic_clear(volatile bool *ptr, int memorder)
744 {
745 #ifdef __ICCARM__
746 STK_STATIC_ASSERT(sizeof(std::__iar_atomic_flag) == sizeof(bool));
747 #endif
748
749 std::__iar_atomic_flag_clear(ptr, memorder);
750 }
751#endif
752
753#ifdef CONTROL_nPRIV_Msk
766__stk_forceinline bool HW_SpinLockTryLock(volatile bool &slock)
767{
768 return !__atomic_test_and_set(&slock, __ATOMIC_ACQUIRE);
769}
770
783__stk_forceinline void HW_SpinLockUnlock(volatile bool &slock)
784{
785 if (slock == false)
786 {
787 STK_KERNEL_PANIC(KERNEL_PANIC_SPINLOCK_DEADLOCK); // release attempt of unowned lock
788 }
789
790 // ensure all data writes (like scheduling metadata) are flushed before the lock is released:
791 // __atomic_clear with __ATOMIC_RELEASE provides the required store-release barrier,
792 // the explicit dmb ishst is retained for toolchains that do not lower __ATOMIC_RELEASE
793 // to a full DMB on ARMv7-M (e.g. older GCC versions with -mcpu=cortex-m4)
794#ifndef __ICCARM__
795 __asm volatile("dmb ishst" ::: "memory");
796#endif
797
798 __atomic_clear(&slock, __ATOMIC_RELEASE);
799}
800#elif defined(RP2040_H) || defined(RP2350_H)
801// Raspberry RP2040 dual-core M0+ implementation, using Hardware Spinlock 0 (SIO base 0xd0000000 + offset)
802#define STK_SIO_SPINLOCK SIO->SPINLOCK31
803
819__stk_forceinline bool HW_SpinLockTryLock(volatile bool &slock)
820{
821 bool success = (STK_SIO_SPINLOCK == 0 ? false : ((slock) = true, true));
822 __stk_dmb();
823
824 return success;
825}
826
843__stk_forceinline void HW_SpinLockUnlock(volatile bool &slock)
844{
845 if (!slock)
846 STK_KERNEL_PANIC(KERNEL_PANIC_SPINLOCK_DEADLOCK); // release attempt of unowned lock
847
848 __stk_dmb();
849 (slock) = false;
850 STK_SIO_SPINLOCK = 1; // writing any value releases the hardware lock
851}
852
853#undef STK_SIO_SPINLOCK
854#else // !RP2040_H
855// Standard single-core Cortex-M0 implementation:
856
870__stk_forceinline bool HW_SpinLockTryLock(volatile bool &slock)
871{
872 const uint32_t ses = HW_CriticalSectionStart();
873
874 if (slock)
875 {
876 HW_CriticalSectionEnd(ses);
877 return false;
878 }
879
880 slock = true;
881 __stk_dmb();
882
883 HW_CriticalSectionEnd(ses);
884 return true;
885}
886
898__stk_forceinline void HW_SpinLockUnlock(volatile bool &slock)
899{
900 if (!slock)
901 STK_KERNEL_PANIC(KERNEL_PANIC_SPINLOCK_DEADLOCK); // release attempt of unowned lock
902
903 __stk_dmb();
904 slock = false;
905}
906#endif // CONTROL_nPRIV_Msk
907
927__stk_forceinline void HW_SpinLockLock(volatile bool &slock)
928{
929 uint32_t timeout_itrs = s_StkSpinlockTimeoutIters;
930 while (!HW_SpinLockTryLock(slock))
931 {
932 if (--timeout_itrs == 0U)
933 {
934 // invariant violated: the lock owner exited without releasing
936 }
937 __stk_relax_cpu();
938 }
939}
940
945__stk_forceinline Word HW_GetCurrentException()
946{
947 return __get_IPSR();
948}
949
952__stk_forceinline bool HW_IsHandlerMode()
953{
954 return (HW_GetCurrentException() != 0U);
955}
956
960__stk_forceinline bool HW_IsPrivilegedContext()
961{
962#ifdef CONTROL_nPRIV_Msk
963 return ((__get_CONTROL() & CONTROL_nPRIV_Msk) == 0U);
964#else
965 return true;
966#endif
967}
968
972__stk_forceinline Word HW_GetCallerSP()
973{
974 // use SP (R13) directly: __get_PSP() returns 0 in unprivileged thread mode
975 Word sp;
976 __asm volatile("MOV %0, SP" : "=r" (sp));
977 return sp;
978}
979
982__stk_forceinline void HW_ScheduleContextSwitch()
983{
984 SCB->ICSR = SCB_ICSR_PENDSVSET_Msk;
985}
986
989__stk_forceinline void HW_ClearFpuState()
990{
991#if STK_CORTEX_M_FPU
992 __set_CONTROL(__get_CONTROL() & ~CONTROL_FPCA_Msk);
993#endif
994}
995
1004void HW_EnableFullFpuAccess()
1005{
1006#if defined(STK_CORTEX_M_FPU) && (STK_CORTEX_M_FPU != 0)
1007
1008 // Compile-time constant bitmasks
1009 constexpr uint32_t SCB_CPACR_CP10_FULL_ACCESS = (3UL << 20U); // enable CP10
1010 constexpr uint32_t SCB_CPACR_CP11_FULL_ACCESS = (3UL << 22U); // enable CP11
1011
1012 /* -------------------------------------------------------------------------- */
1013 /* 1. ARMv7-M & ARMv8-M Base: Grant Privileged & Unprivileged Access */
1014 /* -------------------------------------------------------------------------- */
1015 uint32_t cpacr_val = SCB->CPACR;
1016 cpacr_val |= (SCB_CPACR_CP10_FULL_ACCESS | SCB_CPACR_CP11_FULL_ACCESS);
1017 SCB->CPACR = cpacr_val;
1018
1019#if defined(_STK_CORTEX_M_TRUSTZONE)
1020 constexpr uint32_t SCB_NSACR_CP10_ACCESS = (1UL << 10U); // allow Non-Secure access to CP10
1021 constexpr uint32_t SCB_NSACR_CP11_ACCESS = (1UL << 11U); // allow Non-Secure access to CP11
1022
1023 /* -------------------------------------------------------------------------- */
1024 /* 2. ARMv8-M TrustZone (Secure State Configuration) */
1025 /* -------------------------------------------------------------------------- */
1026
1027 // grant Non-Secure access to CP10 and CP11
1028 uint32_t nsacr_val = SCB->NSACR;
1029 nsacr_val |= (SCB_NSACR_CP10_ACCESS | SCB_NSACR_CP11_ACCESS);
1030 SCB->NSACR = nsacr_val;
1031
1032 // grant Full Access in Non-Secure State
1033 uint32_t ns_cpacr_val = SCB_NS->CPACR;
1034 ns_cpacr_val |= (SCB_CPACR_CP10_FULL_ACCESS | SCB_CPACR_CP11_FULL_ACCESS);
1035 SCB_NS->CPACR = ns_cpacr_val;
1036
1037 // configure Lazy Stacking for Secure & Non-Secure states
1038 uint32_t fpccr_val = FPU->FPCCR;
1039 fpccr_val |= (static_cast<uint32_t>(FPU_FPCCR_ASPEN_Msk) | static_cast<uint32_t>(FPU_FPCCR_LSPEN_Msk));
1040 FPU->FPCCR = fpccr_val;
1041
1042 uint32_t ns_fpccr_val = FPU_NS->FPCCR;
1043 ns_fpccr_val |= (static_cast<uint32_t>(FPU_FPCCR_ASPEN_Msk) | static_cast<uint32_t>(FPU_FPCCR_LSPEN_Msk));
1044 FPU_NS->FPCCR = ns_fpccr_val;
1045
1046#else
1047 /* -------------------------------------------------------------------------- */
1048 /* 3. Non-TrustZone / ARMv7-M FPU Control */
1049 /* -------------------------------------------------------------------------- */
1050 uint32_t fpccr_val = FPU->FPCCR;
1051 fpccr_val |= (static_cast<uint32_t>(FPU_FPCCR_ASPEN_Msk) | static_cast<uint32_t>(FPU_FPCCR_LSPEN_Msk));
1052 FPU->FPCCR = fpccr_val;
1053#endif
1054
1055 // ensure barriers complete FPU configuration before execution proceeds
1056 __DSB();
1057 __ISB();
1058
1059#endif /* defined(STK_CORTEX_M_FPU) && (STK_CORTEX_M_FPU != 0) */
1060}
1061
1065__stk_forceinline uint32_t HW_CoreClockFrequency()
1066{
1067#if STK_CORE_FREQ_UNIFIED
1068 return s_StkSystemCoreClock[0];
1069#else
1070 return s_StkSystemCoreClock[STK_ARCH_GET_CPU_ID()];
1071#endif
1072}
1073
1076__stk_forceinline void HW_ClearPendingSwitch()
1077{
1078 SCB->ICSR = SCB_ICSR_PENDSVCLR_Msk;
1079}
1080
1083__stk_forceinline void HW_SysTickStart(uint32_t period_ticks)
1084{
1085 const Ticks period_ticks_64 = static_cast<Ticks>(period_ticks);
1086
1087 const uint32_t result = SysTick_Config(static_cast<uint32_t>(ConvertTimeUsToClockCycles(HW_CoreClockFrequency(), period_ticks_64)));
1088 STK_ASSERT(result == 0U);
1089 STK_UNUSED(result);
1090
1091 // QEMU workaround (Launchpad Bug #1872237):
1092 // SysTick_Config() writes VAL=0 before setting ENABLE=1. On QEMU,
1093 // systick_reload() silently discards VAL writes while ENABLE=0, leaving
1094 // the internal tick accumulator stale from the previous period;
1095 // writing VAL=0 here, after ENABLE=1 is already set, forces a correct reload.
1096 // This is a no-op on real Cortex-M hardware (spec: ARMv7-M ARM B3.3.1)
1097 SysTick->VAL = 0U;
1098}
1099
1102__stk_forceinline void HW_SysTickStop()
1103{
1104 SysTick->CTRL = 0U;
1105 SCB->ICSR = SCB_ICSR_PENDSTCLR_Msk;
1106}
1107
1110__stk_attr_unused /* can be unused due to configuration */
1111__stk_forceinline void HW_SysTickDisable()
1112{
1113 SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
1114}
1115
1119__stk_forceinline uint32_t HW_SysTickValue()
1120{
1121 return SysTick->VAL;
1122}
1123
1127__stk_attr_unused /* can be unused due to configuration */
1128__stk_forceinline uint32_t HW_SysTickValueAfterDisable()
1129{
1130 // is required for QEMU which then resets SysTick->VAL and thus we can't
1131 // calculate elapsed time correctly
1132 __DSB();
1133
1134 // check for a QEMU case and discard elapsed result
1135 uint32_t ticks = HW_SysTickValue();
1136 if (ticks == 0U)
1137 {
1138 ticks = SysTick->LOAD;
1139 }
1140
1141 return ticks;
1142}
1143
1147__stk_attr_unused /* can be unused due to configuration */
1148__stk_forceinline uint32_t HW_SysTickElapsed(uint32_t ticks)
1149{
1150 return SysTick->LOAD - ticks;
1151}
1152
1156__stk_attr_unused /* can be unused due to configuration */
1157__stk_forceinline void HW_SysTickRearm(uint32_t ticks)
1158{
1159 SysTick->LOAD = ticks - 1U;
1160 SysTick->VAL = 0U;
1161 SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
1162}
1163
1166__stk_forceinline void HW_DWTEnableCounter()
1167{
1168 // enable Trace and Debug blocks (DWT, ITM, ETM, TPIU)
1169#if defined(CoreDebug)
1170 CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
1171#elif defined(DCB)
1172 DCB->DEMCR |= DCB_DEMCR_TRCENA_Msk;
1173#endif
1174 __DSB();
1175
1176 // LAR (Lock Access Register) is mandatory for Cortex-M7 to allow register writes,
1177 // it is typically not implemented or deprecated on M0, M3, M4, and M33
1178#if (__CORTEX_M == 7U)
1179 DWT->LAR = 0xC5ACCE55; // unlock DWT unit using standard CoreSight magic key
1180 __DSB();
1181#endif
1182
1183#if defined(DWT)
1184 // do not interfere with already enabled and running counter
1185 if ((DWT->CTRL & DWT_CTRL_CYCCNTENA_Msk) != DWT_CTRL_CYCCNTENA_Msk)
1186 {
1187 DWT->CYCCNT = 0U; // reset counter value to zero
1188 DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; // start counter
1189 }
1190#endif
1191}
1192
1197__stk_forceinline uint32_t HW_DWTGetCounter()
1198{
1199#if defined(DWT)
1200 return DWT->CYCCNT;
1201#else
1202 return 0U;
1203#endif
1204}
1205
1218void HW_InitSpinlockTimeout()
1219{
1220 const Cycles cycles_budget = ConvertTimeUsToClockCycles(HW_CoreClockFrequency(), STK_SPINLOCK_TIMEOUT_US);
1221 const Cycles iters = cycles_budget / STK_SPINLOCK_MIN_CYCLES_PER_ITER;
1222
1223 // floor guards against a zero/not-yet-configured clock; ceiling guards against overflow
1224 // when narrowing to uint32_t
1225 s_StkSpinlockTimeoutIters = static_cast<uint32_t>(
1226 stk::Min(stk::Max(iters, static_cast<Cycles>(0x1000U)), static_cast<Cycles>(0xFFFFFFFFU)));
1227}
1228
1229// ----------------------------------------------------------------------------
1230namespace drv {
1231// ----------------------------------------------------------------------------
1232
1257
1267struct JmpFrame
1268{
1269 Word R4, R5, R6, R7, R8, R9, R10, R11;
1270 Word SP;
1271 Word LR;
1272#if STK_CORTEX_M_FPU
1273 Word FPSCR;
1274#endif
1275#if STK_CORTEX_M_PAC
1276 Word PAC_R12;
1277#endif
1278};
1279
1295int32_t SaveJmp(JmpFrame &/*f*/)
1296{
1297 __asm volatile(
1299
1300#if (__CORTEX_M >= 3U)
1301 // Cortex-M3/M4/M7: STMIA stores r4-r11 at r0+0 .. r0+28
1302 "STMIA r0, {r4-r11} \n" // store r4-r11 at offsets 0-28, no writeback
1303 "STR SP, [r0, #32] \n" // SP at offset 32
1304 "STR LR, [r0, #36] \n" // LR at offset 36
1305#else
1306 // Cortex-M0/M0+/M1: Thumb-1 only
1307 "STR r4, [r0, #0] \n"
1308 "STR r5, [r0, #4] \n"
1309 "STR r6, [r0, #8] \n"
1310 "STR r7, [r0, #12] \n"
1311 "MOV r1, r8 \n"
1312 "STR r1, [r0, #16] \n"
1313 "MOV r1, r9 \n"
1314 "STR r1, [r0, #20] \n"
1315 "MOV r1, r10 \n"
1316 "STR r1, [r0, #24] \n"
1317 "MOV r1, r11 \n"
1318 "STR r1, [r0, #28] \n"
1319 "MOV r1, SP \n"
1320 "STR r1, [r0, #32] \n"
1321 "MOV r1, LR \n"
1322 "STR r1, [r0, #36] \n"
1323#endif // (__CORTEX_M >= 3U)
1324
1325#if STK_CORTEX_M_FPU
1326 "VMRS r1, FPSCR \n"
1327 "STR r1, [r0, #40] \n"
1328#endif
1329
1330#if STK_CORTEX_M_PAC
1331 "PAC r12, LR, SP \n" // sign LR using SP as modifier -> R12
1332 #if STK_CORTEX_M_FPU
1333 "STR r12, [r0, #44] \n" // offset 44 if FPU is present
1334 #else
1335 "STR r12, [r0, #40] \n" // offset 40 if FPU is absent
1336 #endif
1337#endif
1338
1339 "MOVS r0, #0 \n"
1340 "BX LR \n");
1341
1342#ifdef __ICCARM__
1343#pragma diag_suppress=Pe940
1344 // return value is passed in r0 by the SVC handler per AAPCS;
1345 // IAR cannot see this through the naked asm, suppress the warning.
1346 return 0;
1347#pragma diag_default=Pe940
1348#endif
1349}
1350
1369void RestoreJmp(JmpFrame &/*f*/, int32_t /*val*/)
1370{
1371 __asm volatile(
1373
1374#if (__CORTEX_M >= 3U)
1375 // Cortex-M3/M4/M7: LDMIA loads r4-r11 from offsets 0-28
1376 "LDR sp, [r0, #32] \n" // restore SP
1377
1378#if STK_CORTEX_M_FPU
1379 "LDR r2, [r0, #40] \n" // load saved FPSCR
1380 "VMSR FPSCR, r2 \n" // restore rounding mode + flags
1381#endif
1382
1383#if STK_CORTEX_M_PAC
1384 #if STK_CORTEX_M_FPU
1385 "LDR r12, [r0, #44] \n" // load saved PAC signature into R12
1386 #else
1387 "LDR r12, [r0, #40] \n"
1388 #endif
1389#endif
1390
1391 "LDR r2, [r0, #36] \n" // load saved LR into r2
1392
1393 #if STK_CORTEX_M_PAC
1394 "MOV LR, r2 \n" // move to LR for authentication
1395 "AUT r12, LR, SP \n" // authenticate LR using current SP and R12
1396 "MOV r2, LR \n" // move validated address back to r2
1397 #endif
1398
1399 "LDMIA r0, {r4-r11} \n" // restore r4-r11, no writeback
1400 "MOV r0, r1 \n" // return val
1401 "BX r2 \n"
1402#else
1403 // Cortex-M0/M0+/M1: Thumb-1 only
1404 "LDR r2, [r0, #36] \n"
1405 "MOV LR, r2 \n"
1406 "LDR r2, [r0, #32] \n"
1407 "MOV SP, r2 \n"
1408 "LDR r2, [r0, #28] \n"
1409 "MOV r11, r2 \n"
1410 "LDR r2, [r0, #24] \n"
1411 "MOV r10, r2 \n"
1412 "LDR r2, [r0, #20] \n"
1413 "MOV r9, r2 \n"
1414 "LDR r2, [r0, #16] \n"
1415 "MOV r8, r2 \n"
1416 "LDR r4, [r0, #0] \n"
1417 "LDR r5, [r0, #4] \n"
1418 "LDR r6, [r0, #8] \n"
1419 "LDR r7, [r0, #12] \n"
1420 "MOV r0, r1 \n" // return val
1421 "BX LR \n"
1422#endif // (__CORTEX_M >= 3U)
1423 );
1424}
1425
1426// ----------------------------------------------------------------------------
1427} // namespace drv
1428// ----------------------------------------------------------------------------
1429
1430// ----------------------------------------------------------------------------
1431} // namespace
1432// ----------------------------------------------------------------------------
1433
1434// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1435#if STK_MPU
1436// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1437
1438// ----------------------------------------------------------------------------
1439namespace stk {
1440namespace mpu {
1441// ----------------------------------------------------------------------------
1442
1461struct ScopedPrivilegeBoost
1462{
1468 explicit ScopedPrivilegeBoost();
1469
1476 ~ScopedPrivilegeBoost()
1477 {
1478 // drop privileges and flush pipeline (__set_CONTROL also issues __ISB)
1479 __set_CONTROL(__get_CONTROL() | CONTROL_nPRIV_Msk);
1480 }
1481};
1482
1499ScopedPrivilegeBoost::ScopedPrivilegeBoost()
1500{
1501 // boost privilege via SVC SVC_BOOST_PRIV handler
1502 __asm volatile(
1503 "SVC %0 \n"
1504 : /* no outputs */
1505 : "I"(SVC_BOOST_PRIV)
1506 : "memory"
1507 );
1508}
1509
1510// ----------------------------------------------------------------------------
1511} // namespace stk
1512} // namespace mpu
1513// ----------------------------------------------------------------------------
1514
1515// ----------------------------------------------------------------------------
1516namespace {
1517namespace hw_int {
1518namespace pmsa {
1519// ----------------------------------------------------------------------------
1520
1541void ConfigureRegion(MpuRegion &reg, const struct MpuRegionConfig &cfg, uint32_t region_idx);
1542
1554void ConfigureStatic(const MpuRegionConfig cfg_list[], size_t cfg_count, uint32_t control_flags,
1555 bool non_secure);
1556
1569void ConfigureDynamic(TaskMpu &task_mpu, const struct MpuRegionConfig cfg_list[], const size_t cfg_count,
1570 IPlatform::IEventOverrider *overrider, bool non_secure);
1571
1583void ApplyRegion(const MpuRegion &reg, uint32_t index, bool non_secure);
1584
1592void DisableRegion(uint32_t index, bool non_secure);
1593
1604void Enable(bool enable, uint32_t control_flags, bool non_secure);
1605
1606#if STK_ARCH_ARMV8_M
1614constexpr uint32_t MAIR0_PMSAV8_INIT = 0xFF440400U;
1615
1620constexpr uint32_t MAIR1_PMSAV8_INIT = 0x00000000U;
1621
1626constexpr Word RLAR_ENABLE_FLAG = (1U << 0U);
1627
1636__stk_forceinline Word RBAR_DISABLED_REGION(uint32_t region_idx)
1637{
1638 STK_UNUSED(region_idx);
1639 return SCB->VTOR;
1640}
1641
1647constexpr Word RLAR_DISABLED_REGION = 0U;
1648#else
1653constexpr Word RASR_ENABLE_FLAG = (1U << 0U);
1654
1663constexpr Word RBAR_DISABLED_REGION(uint32_t region_idx) { return (1U << 4U) | (region_idx & 0xFU); }
1664
1670constexpr Word RASR_DISABLED_REGION = 0U;
1671#endif
1672
1673// ----------------------------------------------------------------------------
1674} // namespace pmsa
1675} // namespace hw_int
1676} // namespace
1677// ----------------------------------------------------------------------------
1678
1679// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1680#endif // STK_MPU
1681// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1682
1697#if STK_MPU
1698 extern char __stk_mpu_shared_code_start[];
1699 extern char __stk_mpu_shared_code_end[];
1700 #if defined(DEBUG) || defined(_DEBUG)
1701 extern char __stk_mpu_shared_data_start[];
1702 extern char __stk_mpu_shared_data_end[];
1703 #endif
1704#endif
1705
1706// ----------------------------------------------------------------------------
1707namespace {
1708// ----------------------------------------------------------------------------
1709
1710#if STK_MPU
1716constexpr MpuRegionConfig s_StkDisabledMpuRegion =
1717{
1718 .addr = 0U,
1719 .size = 0U,
1720 .access_perm = hw::mpu::ACCESS_NONE,
1721 .mem_type = hw::mpu::TYPE_STRONGLY_ORDERED,
1722 .share = hw::mpu::SHARE_NON,
1723 .exec = hw::mpu::EXEC_ALLOWED
1724};
1725#endif
1726
1728struct Context final : public PlatformContext
1729{
1730 typedef IPlatform::IEventOverrider eovrd_t;
1731
1732 constexpr Context() : PlatformContext(), m_exit_buf(), m_overrider(nullptr),
1733 #if STK_CORTEX_M_TRUSTZONE_FRAME
1734 m_overrider_ns(nullptr),
1735 #endif
1736 #if STK_TICKLESS_IDLE
1737 m_sleep_ticks(0), m_sleep_error(0U),
1738 #endif
1739 m_csu(0U), m_csu_nesting(0U), m_started(false), m_exiting(false)
1740 {}
1741
1742 void Initialize(IPlatform::IEventHandler *handler, IKernelService *service, Stack *exit_trap,
1743 uint32_t resolution_us)
1744 {
1745 InitializeBase(handler, service, exit_trap, resolution_us);
1746
1747 STK_STATIC_ASSERT_DESC_N(SP, offsetof(Stack, SP) == 0U,
1748 "expect Stack::SP member at offset of 0 (first member)");
1749 #if STK_MPU
1751 (STK_MPU_TASK_REGIONS <= 4U)
1752 ? ((STK_CORTEX_M_MPU_TASK_REGION_IDX % 4U) <= (4U - STK_MPU_TASK_REGIONS))
1753 : ((STK_CORTEX_M_MPU_TASK_REGION_IDX % 4U) == 0U),
1754 "STK_CORTEX_M_MPU_TASK_REGION_IDX is not aligned correctly for the RNR-relative "
1755 "alias burst write at the configured STK_MPU_TASK_REGIONS width");
1756 STK_STATIC_ASSERT_DESC_N(mpu_task_regions,
1757 (STK_MPU_TASK_REGIONS == 2U) || (STK_MPU_TASK_REGIONS == 4U) || (STK_MPU_TASK_REGIONS == 6U) ||
1758 (STK_MPU_TASK_REGIONS == 8U) || (STK_MPU_TASK_REGIONS == 12U) || (STK_MPU_TASK_REGIONS == 16U),
1759 "STK_MPU_TASK_REGIONS must be defined as 2, 4, 6, 8, 12 or 16");
1760 #ifdef _STK_CORTEX_M_TRUSTZONE
1763 ? ((STK_CORTEX_M_MPU_TASK_REGION_IDX_NS % 4U) <= (4U - STK_MPU_TASK_REGIONS_NS))
1764 : ((STK_CORTEX_M_MPU_TASK_REGION_IDX_NS % 4U) == 0U),
1765 "STK_CORTEX_M_MPU_TASK_REGION_IDX_NS is not aligned correctly for the RNR-relative "
1766 "alias burst write at the configured STK_MPU_TASK_REGIONS_NS width");
1767 STK_STATIC_ASSERT_DESC_N(mpu_task_regions_ns,
1770 "STK_MPU_TASK_REGIONS_NS must be defined as 2, 4, 6, 8, 12 or 16");
1771 #endif
1772 // make sure linker declares __stk_mpu_shared_xxx regions correctly, i.e. code and data
1773 // sections must contain data
1774 STK_ASSERT(hw::PtrToWord(__stk_mpu_shared_code_end) - hw::PtrToWord(__stk_mpu_shared_code_start) > 0U);
1775 STK_ASSERT(hw::PtrToWord(__stk_mpu_shared_data_end) - hw::PtrToWord(__stk_mpu_shared_data_start) > 0U);
1776 #endif
1777 STK_ASSERT(s_StkCortexmCsuLock == false);
1778 STK_ASSERT(g_StkLastPanicId == KERNEL_PANIC_NONE);
1779
1780 m_csu = 0U;
1781 m_csu_nesting = 0U;
1782 m_started = false;
1783 m_exiting = false;
1784 #if STK_TICKLESS_IDLE
1785 m_sleep_ticks = 0;
1786 m_sleep_error = 0U;
1787 #endif
1788
1789 // initialize system clock if user-side code has not yet done it
1790 if (s_StkSystemCoreClock[0] == 0U)
1791 {
1792 for (uint8_t i = 0U; i < STK_STATIC_ARRAY_SIZE(s_StkSystemCoreClock); ++i)
1793 {
1794 s_StkSystemCoreClock[i] = SystemCoreClock;
1795 }
1796 }
1797
1798 HW_InitSpinlockTimeout();
1799
1800 #if (__CORTEX_M > 1) && STK_TICKLESS_USE_ARM_DWT
1801 HW_DWTEnableCounter();
1802 #endif
1803
1804 #if STK_SEGGER_SYSVIEW
1805 SEGGER_SYSVIEW_Init(
1806 HW_CoreClockFrequency(),
1807 HW_CoreClockFrequency(),
1808 nullptr,
1809 drv::SendSysDesc);
1810 #endif
1811 }
1812
1813#if STK_TICKLESS_IDLE
1814 __stk_forceinline void OnTick(Timeout &ticks)
1815#else
1816 __stk_forceinline void OnTick()
1817#endif
1818 {
1819 if (m_handler->OnTick(m_stack_idle, m_stack_active
1820 #if STK_TICKLESS_IDLE
1821 , ticks
1822 #endif
1823 ))
1824 {
1825 HW_ScheduleContextSwitch();
1826 }
1827 }
1828
1829 __stk_forceinline void ProcessTick()
1830 {
1831 HW_DisableInterrupts();
1832
1833 #if STK_TICKLESS_IDLE
1834 Timeout ticks = m_sleep_ticks;
1835
1836 OnTick(ticks);
1837
1838 // re-arm SysTick only if tick period changed
1839 if (ticks != m_sleep_ticks)
1840 {
1841 m_sleep_ticks = ReloadTickPeriod(ticks);
1842 }
1843 #else
1844 OnTick();
1845 #endif // STK_TICKLESS_IDLE
1846
1847 HW_EnableInterrupts();
1848 }
1849
1850 __stk_forceinline void OnForceContextSwitch(TId id)
1851 {
1852 if (m_handler->OnForceContextSwitch(id, m_stack_idle, m_stack_active))
1853 {
1854 HW_ScheduleContextSwitch();
1855 }
1856 }
1857
1858 __stk_forceinline void OnEnterCriticalSection(uint32_t current_ses)
1859 {
1860 if (m_csu_nesting == 0U)
1861 {
1862 // ONLY attempt the global spinlock if we aren't already nested
1863 HW_SpinLockLock(s_StkCortexmCsuLock);
1864
1865 // store the hardware interrupt state to restore later
1866 m_csu = current_ses;
1867 }
1868
1869 // increase nesting count within a limit
1870 if (++m_csu_nesting > STK_CS_NESTINGS_MAX)
1871 {
1872 // invariant violated: exceeded max allowed number of recursions
1873 STK_KERNEL_PANIC(KERNEL_PANIC_CS_NESTING_OVERFLOW);
1874 }
1875 }
1876
1877 __stk_forceinline bool OnExitCriticalSection(uint32_t &restore_ses)
1878 {
1879 bool released = false;
1880
1881 if (m_csu_nesting == 0U)
1882 {
1883 // invariant violated: unbalanced Exit, uint8_t would wrap to 255 and the lock would never release
1884 STK_KERNEL_PANIC(KERNEL_PANIC_CS_NESTING_OVERFLOW);
1885 }
1886 --m_csu_nesting;
1887
1888 if (m_csu_nesting == 0U)
1889 {
1890 // capture the state before releasing lock
1891 restore_ses = m_csu;
1892
1893 // release global lock
1894 HW_SpinLockUnlock(s_StkCortexmCsuLock);
1895
1896 released = true;
1897 }
1898
1899 return released;
1900 }
1901
1902 __stk_forceinline void EnterCriticalSection()
1903 {
1904 const uint32_t current_ses = HW_CriticalSectionStart();
1905
1906 OnEnterCriticalSection(current_ses);
1907 }
1908
1909 __stk_forceinline void ExitCriticalSection()
1910 {
1911 uint32_t restore_ses;
1912 if (OnExitCriticalSection(restore_ses))
1913 {
1914 HW_CriticalSectionEnd(restore_ses);
1915 }
1916 }
1917
1918 void StartTickTimer(Timeout elapsed_ticks)
1919 {
1920 #if STK_TICKLESS_IDLE
1921 // reset sleep ticks if kernel was restarted
1922 m_sleep_ticks = elapsed_ticks;
1923 #else
1924 STK_UNUSED(elapsed_ticks);
1925 #endif
1926
1927 // start SysTick timer (it is yet can't fire an interrupt due to HW_DisableInterrupts)
1928 HW_SysTickStart(m_tick_resolution);
1929
1930 // note: Always after SysTick_Config because it may change SysTick priority
1931 NVIC_SetPriority(SysTick_IRQn, STK_CORTEX_M_ISR_PRIORITY_LOWEST);
1932 }
1933
1934#if STK_TLS && !STK_INLINE_TLS
1935 Word GetTls()
1936 {
1937 hw::CriticalSection::ScopedLock cs_;
1938
1939 STK_ASSERT(m_stack_active != nullptr);
1940
1941 return m_stack_active->tls;
1942 }
1943
1944 void SetTls(Word tp)
1945 {
1946 hw::CriticalSection::ScopedLock cs_;
1947
1948 STK_ASSERT(m_stack_active != nullptr);
1949
1950 m_stack_active->tls = tp;
1951 }
1952#endif // STK_TLS && !STK_INLINE_TLS
1953
1954 void OnSleepOverride()
1955 {
1956 #if STK_TICKLESS_IDLE
1957 const Timeout sleep_ticks = m_sleep_ticks;
1958 #else
1959 const Timeout sleep_ticks = 1;
1960 #endif
1961
1962 if (!m_overrider->OnSleep(sleep_ticks))
1963 {
1964 HW_EnterSleepMode();
1965 }
1966 }
1967
1968 uint32_t GetTickResolutionInClockCycles()
1969 {
1970 const Ticks resolution_ticks = static_cast<Ticks>(m_tick_resolution);
1971 return static_cast<uint32_t>(ConvertTimeUsToClockCycles(HW_CoreClockFrequency(), resolution_ticks));
1972 }
1973
1974#if STK_MPU
1975 void ConfigureMpuInstance(eovrd_t *overrider, bool non_secure)
1976 {
1977 // configure Secure binary side if TrustZone enabled, or just standard
1978 if (overrider != nullptr)
1979 {
1980 const MpuConfig *const cfg = overrider->OnConfigureMpu();
1981
1982 // OnConfigureMpu() may legitimately return nullptr (no custom global config supplied),
1983 // treat that the same as MPU_CFG_NONE below.
1984 if (cfg != nullptr)
1985 {
1986 #if STK_CORTEX_M_TRUSTZONE_FRAME
1987 // MPU_CFG_NONSECURE_MPU selects which hardware MPU instance a config targets
1988 // (absent = Secure, set = Non-Secure). It must agree with the instance this call
1989 // is actually configuring (see ConfigureMpu() below), catching a config wired to
1990 // the wrong overrider (e.g. Secure flags returned from the Non-Secure overrider).
1991 STK_ASSERT(non_secure == ((cfg->mode & hw::mpu::MPU_CFG_NONSECURE_MPU) != 0U));
1992 #endif
1993
1994 if ((cfg->mode & hw::mpu::MPU_CFG_CLEAR_ON_INIT) != 0U)
1995 {
1996 // wipe every hardware region slot (static block + per-task block) so no stale
1997 // configuration left behind by a previous run/bootloader survives the static
1998 // table applied by ConfigureStatic() below.
1999 for (uint32_t index = 0U; index < STK_CORTEX_M_MPU_REGIONS_MAX; ++index)
2000 {
2001 hw_int::pmsa::DisableRegion(index, non_secure);
2002 }
2003 }
2004
2005 uint32_t control_flags = 0U;
2006
2007 if ((cfg->mode & hw::mpu::MPU_CFG_PRIVILEGED_BG_MEM) != 0U)
2008 {
2009 control_flags |= MPU_CTRL_PRIVDEFENA_Msk;
2010 }
2011
2012 if ((cfg->mode & hw::mpu::MPU_CFG_IN_FAULTS) != 0U)
2013 {
2014 control_flags |= MPU_CTRL_HFNMIENA_Msk;
2015 }
2016
2017 hw_int::pmsa::ConfigureStatic(cfg->regions.GetPtr(), cfg->regions.GetSize(), control_flags, non_secure);
2018 }
2019 else
2020 {
2021 hw_int::pmsa::Enable(false, 0, non_secure);
2022 }
2023 }
2024 }
2025#endif // STK_MPU
2026
2027#if STK_MPU
2028 void ConfigureMpu()
2029 {
2030 // configure Secure binary side if TrustZone enabled, or just standard
2031 ConfigureMpuInstance(m_overrider, false);
2032
2033 #if STK_CORTEX_M_TRUSTZONE_FRAME
2034 // configure MPU_NS for Non-Secure binary side
2035 ConfigureMpuInstance(m_overrider_ns, true);
2036 #endif
2037 }
2038#endif // STK_MPU
2039
2040 TId GetCurrentTId() const
2041 {
2042 #if STK_STACK_NEEDS_TASK_ID
2043 const TId tid = (m_stack_active != nullptr ? m_stack_active->tid : TID_NONE);
2044 #else
2045 const TId tid = TID_NONE;
2046 #endif
2047
2048 return tid;
2049 }
2050
2051 void Start();
2052 void OnStart();
2053 void OnStop();
2054#if STK_TICKLESS_IDLE
2055 Timeout ReloadTickPeriod(Timeout ticks_requested);
2056 Timeout Suspend();
2057 void Resume(Timeout elapsed_ticks);
2058#endif
2059
2060 drv::JmpFrame m_exit_buf;
2061 eovrd_t *m_overrider;
2062#if STK_CORTEX_M_TRUSTZONE_FRAME
2063 eovrd_t *m_overrider_ns;
2064#endif
2065#if STK_TICKLESS_IDLE
2066 Timeout m_sleep_ticks;
2067 uint32_t m_sleep_error;
2068#endif
2069 uint32_t m_csu;
2070 uint8_t m_csu_nesting;
2071 volatile bool m_started;
2072 bool m_exiting;
2073};
2074
2079Context s_StkPlatformContext[STK_ARCH_CPU_COUNT];
2080
2081// ----------------------------------------------------------------------------
2082} // namespace
2083// ----------------------------------------------------------------------------
2084
2085__stk_attr_noinline // keep out of inlining to preserve stack frame
2086__stk_attr_noreturn // never returns - a trap
2088{
2089 g_StkLastPanicId = id;
2090
2091 // disable all maskable interrupts: this prevents scheduler from running again and corrupting state further
2092 HW_DisableInterrupts();
2093
2094 // spin forever: with a watchdog active this produces a clean reset, without a watchdog,
2095 // a debugger can attach and inspect 'id'
2096 for (;;)
2097 {
2098 __stk_relax_cpu();
2099 }
2100}
2101
2103{
2104 GetContext().ProcessTick();
2105}
2106
2107#if STK_TICKLESS_IDLE
2108Timeout Context::ReloadTickPeriod(Timeout ticks_requested)
2109{
2110 Timeout clamped_ticks;
2111
2112 const uint32_t SYSTICK_MAX_LOAD = 0x00FFFFFFU; // SysTick LOAD register is 24-bit
2113 const uint32_t tick_resolution = GetTickResolutionInClockCycles();
2114 if (tick_resolution != 0U)
2115 {
2116 // guard against uint32_t overflow in the reload calculation
2117 STK_ASSERT((static_cast<uint64_t>(ticks_requested) * tick_resolution) <= UINT32_MAX);
2118
2119 // clamp ticks_requested so that cpu_ticks_requested fits into 24-bit SysTick LOAD register
2120 // without clamping large sleep tick counts silently truncate LOAD, causing the timer to fire far too early
2121 // breaking the timing
2122 const uint32_t max_ticks = SYSTICK_MAX_LOAD / tick_resolution;
2123 if (ticks_requested > static_cast<Timeout>(max_ticks))
2124 {
2125 clamped_ticks = static_cast<Timeout>(max_ticks);
2126 }
2127 else
2128 {
2129 clamped_ticks = ticks_requested;
2130 }
2131
2132 // start counting how many CPU cycles further instructions take until SysTick timer is enabled again;
2133 // without DWT we will have tick error of around 80 cycles depending on CPU model and compiler optimization
2134 #if (__CORTEX_M > 1) && STK_TICKLESS_USE_ARM_DWT
2135 const uint32_t error = HW_DWTGetCounter();
2136 __stk_compiler_barrier(); // prevent reordering, we measure all cycles of instructions below this point
2137 #endif
2138
2139 // pause SysTick
2140 HW_SysTickDisable();
2141
2142 // get already elapsed CPU cycles since SysTick ISR invocation up to SysTick timer stop (see above)
2143 // to account for them for a new period value
2144 const uint32_t elapsed_till_stop = HW_SysTickElapsed(HW_SysTickValueAfterDisable());
2145
2146 // OnTick() should not consume more than next period
2147 STK_ASSERT(static_cast<Timeout>(elapsed_till_stop / tick_resolution) <= clamped_ticks);
2148
2149 const uint32_t cpu_ticks_requested = static_cast<uint32_t>(clamped_ticks) * tick_resolution;
2150
2151 // substract number of cycles elapsed till SysTick stop + error from previous round
2152 uint32_t new_load = cpu_ticks_requested - elapsed_till_stop - m_sleep_error;
2153
2154 // clamp: rearm overhead must never push new_load into underflow
2155 if (new_load > cpu_ticks_requested)
2156 {
2157 new_load = cpu_ticks_requested;
2158 }
2159
2160 // reload with elapsed ticks accounted
2161 HW_SysTickRearm(new_load);
2162
2163 #if (__CORTEX_M > 1) && STK_TICKLESS_USE_ARM_DWT
2164 // calculate error: subtract cycles consumed by the rearm sequence itself in the next round
2165 m_sleep_error = HW_DWTGetCounter() - error;
2166 #endif
2167 }
2168 else
2169 {
2170 STK_ASSERT(false);
2171 clamped_ticks = NO_WAIT;
2172 }
2173
2174 // return actual clamped ticks armed
2175 return clamped_ticks;
2176}
2177#endif // STK_TICKLESS_IDLE
2178
2179extern "C" void STK_SYSTICK_HANDLER()
2180{
2181 // start tracing
2182#if STK_SEGGER_SYSVIEW
2183 SEGGER_SYSVIEW_RecordEnterISR();
2184#endif
2185
2186 Context &ctx = GetContext();
2187
2188#ifdef HAL_MODULE_ENABLED // STM32 HAL
2189 // make sure STM32 HAL gets timing information as it depends on SysTick in delaying procedures
2190#if STK_TICKLESS_IDLE
2191 const uint32_t sleep_ticks = static_cast<uint32_t>(ctx.m_sleep_ticks);
2192 uwTick += (sleep_ticks * ctx.m_tick_resolution);
2193#else
2194 HAL_IncTick();
2195#endif
2196
2197 // STM32 HAL starts SysTick on its initialization that will cause a crash on NULL,
2198 // therefore use additional check if HAL_MODULE_ENABLED is defined
2199 if (ctx.m_started)
2200 {
2201#else
2202 {
2203 // make sure SysTick is enabled by the Kernel::Start(), disable its start anywhere else
2204 STK_ASSERT(ctx.m_started);
2205 STK_ASSERT(ctx.m_handler != nullptr);
2206#endif
2207 ctx.ProcessTick();
2208 }
2209
2210 // stop tracing & send packets to host
2211#if STK_SEGGER_SYSVIEW
2212 SEGGER_SYSVIEW_RecordExitISR();
2213 SEGGER_SYSVIEW_IsStarted();
2214#endif
2215}
2216
2217#if STK_SEGGER_SYSVIEW
2218 #define STK_SYSVIEW_CALL(func)\
2219 "PUSH {r0-r3, r7, r11, r12, LR} \n"\
2220 "BL " #func " \n"\
2221 "POP {r0-r3, r7, r11, r12, LR} \n"
2222#endif
2223
2224#if STK_SEGGER_SYSVIEW
2225extern "C" __stk_attr_used void StkSystemView_OnContextSwitch(void)
2226{
2227 Context &ctx = GetContext();
2228
2229 // exit from PendSV
2230 SEGGER_SYSVIEW_RecordExitISR();
2231
2232 if (ctx.m_stack_idle->tid != SYS_TASK_ID_SLEEP)
2233 {
2234 // previous task went idle
2235 SEGGER_SYSVIEW_OnTaskStopReady(ctx.m_stack_idle->tid, TRACE_EVENT_SWITCH);
2236 }
2237
2238 if (ctx.m_stack_active->tid != SYS_TASK_ID_SLEEP)
2239 {
2240 // current task resumed execution
2241 SEGGER_SYSVIEW_OnTaskStartExec(ctx.m_stack_active->tid);
2242 }
2243}
2244#endif
2245
2273#if (STK_MPU_TASK_REGIONS == 2U)
2274#define STK_ASM_BLOCK_MPU_S_STACK_GUARD\
2275 "LDR r0, =%[mpu_start_of] \n" /* r0 = &MPU->RBAR */\
2276 "ADD r12, r1, %[mpu_reg_of] \n" /* point to mpu.region[0] */\
2277 "LDMIA r12, {r4-r7} \n" /* burst load 4 words */\
2278 "STMIA r0, {r4-r7} \n" /* write MPU RBAR..RLAR_A1 (r0 remains anchored) */
2279#elif (STK_MPU_TASK_REGIONS == 4U)
2280#define STK_ASM_BLOCK_MPU_S_STACK_GUARD\
2281 "LDR r0, =%[mpu_start_of] \n" /* r0 = &MPU->RBAR */\
2282 "ADD r12, r1, %[mpu_reg_of] \n" /* point to mpu.region[0] */\
2283 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2284 "STMIA r0, {r4-r11} \n" /* write MPU RBAR..RLAR_A3 (r0 remains anchored) */
2285#elif (STK_MPU_TASK_REGIONS == 6U)
2286#define STK_ASM_BLOCK_MPU_S_STACK_GUARD\
2287 "LDR r0, =%[mpu_start_of] \n" /* r0 = &MPU->RBAR */\
2288 \
2289 /* Block 1 (Regions 0-3 / 8 words) */\
2290 "ADD r12, r1, %[mpu_reg_of] \n" /* point to mpu.region[0] */\
2291 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2292 "STMIA r0, {r4-r11} \n" /* write MPU RBAR..RLAR_A3 */\
2293 \
2294 /* Block 2 (Regions 4-5 / 4 words) */\
2295 "LDR r12, =%[mpu_rnr_idx4] \n" /* r12 = IDX + 4 */\
2296 "STR r12, [r0, #-4] \n" /* MPU->RNR = IDX + 4 (RNR is RBAR - 4) */\
2297 "ADD r12, r1, %[mpu_reg4_of] \n" /* point to mpu.region[4] */\
2298 "LDMIA r12, {r4-r7} \n" /* burst load 4 words */\
2299 "STMIA r0, {r4-r7} \n" /* write MPU RBAR..RLAR_A1 */\
2300 \
2301 /* Restore RNR */\
2302 "LDR r12, =%[mpu_rnr_idx] \n" /* r12 = IDX */\
2303 "STR r12, [r0, #-4] \n" /* restore MPU->RNR */
2304#elif (STK_MPU_TASK_REGIONS == 8U)
2305#define STK_ASM_BLOCK_MPU_S_STACK_GUARD\
2306 "LDR r0, =%[mpu_start_of] \n" /* r0 = &MPU->RBAR */\
2307 \
2308 /* Block 1 (Regions 0-3 / 8 words) */\
2309 "ADD r12, r1, %[mpu_reg_of] \n" /* point to mpu.region[0] */\
2310 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2311 "STMIA r0, {r4-r11} \n" /* write MPU RBAR..RLAR_A3 */\
2312 \
2313 /* Block 2 (Regions 4-7 / 8 words) */\
2314 "LDR r12, =%[mpu_rnr_idx4] \n" /* r12 = IDX + 4 */\
2315 "STR r12, [r0, #-4] \n" /* MPU->RNR = IDX + 4 (RNR is RBAR - 4) */\
2316 "ADD r12, r1, %[mpu_reg4_of] \n" /* point to mpu.region[4] */\
2317 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2318 "STMIA r0, {r4-r11} \n" /* write MPU RBAR..RLAR_A3 */\
2319 \
2320 /* Restore RNR */\
2321 "LDR r12, =%[mpu_rnr_idx] \n" /* r12 = IDX */\
2322 "STR r12, [r0, #-4] \n" /* restore MPU->RNR */
2323#elif (STK_MPU_TASK_REGIONS == 12U)
2324#define STK_ASM_BLOCK_MPU_S_STACK_GUARD\
2325 "LDR r0, =%[mpu_start_of] \n" /* r0 = &MPU->RBAR */\
2326 \
2327 /* Block 1 (Regions 0-3 / 8 words) */\
2328 "ADD r12, r1, %[mpu_reg_of] \n" /* point to mpu.region[0] */\
2329 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2330 "STMIA r0, {r4-r11} \n" /* write MPU RBAR..RLAR_A3 */\
2331 \
2332 /* Block 2 (Regions 4-7 / 8 words) */\
2333 "LDR r12, =%[mpu_rnr_idx4] \n" /* r12 = IDX + 4 */\
2334 "STR r12, [r0, #-4] \n" /* MPU->RNR = IDX + 4 (RNR is RBAR - 4) */\
2335 "ADD r12, r1, %[mpu_reg4_of] \n" /* point to mpu.region[4] */\
2336 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2337 "STMIA r0, {r4-r11} \n" /* write MPU RBAR..RLAR_A3 */\
2338 \
2339 /* Block 3 (Regions 8-11 / 8 words) */\
2340 "LDR r12, =%[mpu_rnr_idx8] \n" /* r12 = IDX + 8 */\
2341 "STR r12, [r0, #-4] \n" /* MPU->RNR = IDX + 8 */\
2342 "ADD r12, r1, %[mpu_reg8_of] \n" /* point to mpu.region[8] */\
2343 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2344 "STMIA r0, {r4-r11} \n" /* write MPU RBAR..RLAR_A3 */\
2345 \
2346 /* Restore RNR */\
2347 "LDR r12, =%[mpu_rnr_idx] \n" /* r12 = IDX */\
2348 "STR r12, [r0, #-4] \n" /* restore MPU->RNR */
2349#elif (STK_MPU_TASK_REGIONS == 16U)
2350#define STK_ASM_BLOCK_MPU_S_STACK_GUARD\
2351 "LDR r0, =%[mpu_start_of] \n" /* r0 = &MPU->RBAR */\
2352 \
2353 /* Block 1 (Regions 0-3 / 8 words) */\
2354 "ADD r12, r1, %[mpu_reg_of] \n" /* point to mpu.region[0] */\
2355 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2356 "STMIA r0, {r4-r11} \n" /* write MPU RBAR..RLAR_A3 */\
2357 \
2358 /* Block 2 (Regions 4-7 / 8 words) */\
2359 "LDR r12, =%[mpu_rnr_idx4] \n" /* r12 = IDX + 4 */\
2360 "STR r12, [r0, #-4] \n" /* MPU->RNR = IDX + 4 (RNR is RBAR - 4) */\
2361 "ADD r12, r1, %[mpu_reg4_of] \n" /* point to mpu.region[4] */\
2362 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2363 "STMIA r0, {r4-r11} \n" /* write MPU RBAR..RLAR_A3 */\
2364 \
2365 /* Block 3 (Regions 8-11 / 8 words) */\
2366 "LDR r12, =%[mpu_rnr_idx8] \n" /* r12 = IDX + 8 */\
2367 "STR r12, [r0, #-4] \n" /* MPU->RNR = IDX + 8 */\
2368 "ADD r12, r1, %[mpu_reg8_of] \n" /* point to mpu.region[8] */\
2369 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2370 "STMIA r0, {r4-r11} \n" /* write MPU RBAR..RLAR_A3 */\
2371 \
2372 /* Block 4 (Regions 12-15 / 8 words) */\
2373 "LDR r12, =%[mpu_rnr_idx12] \n" /* r12 = IDX + 12 */\
2374 "STR r12, [r0, #-4] \n" /* MPU->RNR = IDX + 12 */\
2375 "ADD r12, r1, %[mpu_reg12_of] \n" /* point to mpu.region[12] */\
2376 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2377 "STMIA r0, {r4-r11} \n" /* write MPU RBAR..RLAR_A3 */\
2378 \
2379 /* Restore RNR */\
2380 "LDR r12, =%[mpu_rnr_idx] \n" /* r12 = IDX */\
2381 "STR r12, [r0, #-4] \n" /* restore MPU->RNR */
2382#else
2383 #error "Unsupported region count, allowed - 2, 4, 6, 8, 12, 16!"
2384#endif
2385
2386#if STK_CORTEX_M_TRUSTZONE_FRAME
2387 #if (STK_MPU_TASK_REGIONS_NS == 2U)
2388 #define STK_ASM_BLOCK_MPU_NS_STACK_GUARD\
2389 \
2390 /* Non-Secure Dual MPU (2 regions / 4 words) */\
2391 "LDR r0, =%[mpu_ns_start_of] \n" /* r0 = &MPU_NS->RBAR */\
2392 "ADD r12, r1, %[mpu_ns_reg_of] \n" /* point to mpu_ns.region[0] */\
2393 "LDMIA r12, {r4-r7} \n" /* burst load 4 words */\
2394 "STMIA r0, {r4-r7} \n" /* write MPU_NS RBAR..RLAR_A1 */
2395 #elif (STK_MPU_TASK_REGIONS_NS == 4U)
2396 #define STK_ASM_BLOCK_MPU_NS_STACK_GUARD\
2397 \
2398 /* Non-Secure Dual MPU (4 regions / 8 words) */\
2399 "LDR r0, =%[mpu_ns_start_of] \n" /* r0 = &MPU_NS->RBAR */\
2400 "ADD r12, r1, %[mpu_ns_reg_of] \n" /* point to mpu_ns.region[0] */\
2401 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2402 "STMIA r0, {r4-r11} \n" /* write MPU_NS RBAR..RLAR_A3 */
2403 #elif (STK_MPU_TASK_REGIONS_NS == 6U)
2404 #define STK_ASM_BLOCK_MPU_NS_STACK_GUARD\
2405 \
2406 /* Non-Secure Dual MPU (6 regions) */\
2407 "LDR r0, =%[mpu_ns_start_of] \n" /* r0 = &MPU_NS->RBAR */\
2408 \
2409 /* NS Block 1 (Regions 0-3 / 8 words) */\
2410 "ADD r12, r1, %[mpu_ns_reg_of] \n" /* point to mpu_ns.region[0] */\
2411 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2412 "STMIA r0, {r4-r11} \n" /* write MPU_NS RBAR..RLAR_A3 */\
2413 \
2414 /* NS Block 2 (Regions 4-5 / 4 words) */\
2415 "LDR r12, =%[mpu_ns_rnr_idx4] \n" /* r12 = IDX + 4 */\
2416 "STR r12, [r0, #-4] \n" /* MPU_NS->RNR = IDX + 4 */\
2417 "ADD r12, r1, %[mpu_ns_reg4_of]\n" /* point to mpu_ns.region[4] */\
2418 "LDMIA r12, {r4-r7} \n" /* burst load 4 words */\
2419 "STMIA r0, {r4-r7} \n" /* write MPU_NS RBAR..RLAR_A1 */\
2420 \
2421 /* Restore NS RNR */\
2422 "LDR r12, =%[mpu_ns_rnr_idx] \n" /* r12 = IDX */\
2423 "STR r12, [r0, #-4] \n" /* restore MPU_NS->RNR */
2424 #elif (STK_MPU_TASK_REGIONS_NS == 8U)
2425 #define STK_ASM_BLOCK_MPU_NS_STACK_GUARD\
2426 \
2427 /* Non-Secure Dual MPU (8 regions) */\
2428 "LDR r0, =%[mpu_ns_start_of] \n" /* r0 = &MPU_NS->RBAR */\
2429 \
2430 /* NS Block 1 (Regions 0-3 / 8 words) */\
2431 "ADD r12, r1, %[mpu_ns_reg_of] \n" /* point to mpu_ns.region[0] */\
2432 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2433 "STMIA r0, {r4-r11} \n" /* write MPU_NS RBAR..RLAR_A3 */\
2434 \
2435 /* NS Block 2 (Regions 4-7 / 8 words) */\
2436 "LDR r12, =%[mpu_ns_rnr_idx4] \n" /* r12 = IDX + 4 */\
2437 "STR r12, [r0, #-4] \n" /* MPU_NS->RNR = IDX + 4 */\
2438 "ADD r12, r1, %[mpu_ns_reg4_of]\n" /* point to mpu_ns.region[4] */\
2439 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2440 "STMIA r0, {r4-r11} \n" /* write MPU_NS RBAR..RLAR_A3 */\
2441 \
2442 /* Restore NS RNR */\
2443 "LDR r12, =%[mpu_ns_rnr_idx] \n" /* r12 = IDX */\
2444 "STR r12, [r0, #-4] \n" /* restore MPU_NS->RNR */
2445 #elif (STK_MPU_TASK_REGIONS_NS == 12U)
2446 #define STK_ASM_BLOCK_MPU_NS_STACK_GUARD\
2447 \
2448 /* Non-Secure Dual MPU (12 regions / 3 blocks) */\
2449 "LDR r0, =%[mpu_ns_start_of] \n" /* r0 = &MPU_NS->RBAR */\
2450 \
2451 /* NS Block 1 (Regions 0-3 / 8 words) */\
2452 "ADD r12, r1, %[mpu_ns_reg_of] \n" /* point to mpu_ns.region[0] */\
2453 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2454 "STMIA r0, {r4-r11} \n" /* write MPU_NS RBAR..RLAR_A3 */\
2455 \
2456 /* NS Block 2 (Regions 4-7 / 8 words) */\
2457 "LDR r12, =%[mpu_ns_rnr_idx4] \n" /* r12 = IDX + 4 */\
2458 "STR r12, [r0, #-4] \n" /* MPU_NS->RNR = IDX + 4 */\
2459 "ADD r12, r1, %[mpu_ns_reg4_of]\n" /* point to mpu_ns.region[4] */\
2460 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2461 "STMIA r0, {r4-r11} \n" /* write MPU_NS RBAR..RLAR_A3 */\
2462 \
2463 /* NS Block 3 (Regions 8-11 / 8 words) */\
2464 "LDR r12, =%[mpu_ns_rnr_idx8] \n" /* r12 = IDX + 8 */\
2465 "STR r12, [r0, #-4] \n" /* MPU_NS->RNR = IDX + 8 */\
2466 "ADD r12, r1, %[mpu_ns_reg8_of]\n" /* point to mpu_ns.region[8] */\
2467 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2468 "STMIA r0, {r4-r11} \n" /* write MPU_NS RBAR..RLAR_A3 */\
2469 \
2470 /* Restore NS RNR */\
2471 "LDR r12, =%[mpu_ns_rnr_idx] \n" /* r12 = IDX */\
2472 "STR r12, [r0, #-4] \n" /* restore MPU_NS->RNR */
2473 #elif (STK_MPU_TASK_REGIONS_NS == 16U)
2474 #define STK_ASM_BLOCK_MPU_NS_STACK_GUARD\
2475 \
2476 /* Non-Secure Dual MPU (16 regions / 4 blocks) */\
2477 "LDR r0, =%[mpu_ns_start_of] \n" /* r0 = &MPU_NS->RBAR */\
2478 \
2479 /* NS Block 1 (Regions 0-3 / 8 words) */\
2480 "ADD r12, r1, %[mpu_ns_reg_of] \n" /* point to mpu_ns.region[0] */\
2481 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2482 "STMIA r0, {r4-r11} \n" /* write MPU_NS RBAR..RLAR_A3 */\
2483 \
2484 /* NS Block 2 (Regions 4-7 / 8 words) */\
2485 "LDR r12, =%[mpu_ns_rnr_idx4] \n" /* r12 = IDX + 4 */\
2486 "STR r12, [r0, #-4] \n" /* MPU_NS->RNR = IDX + 4 */\
2487 "ADD r12, r1, %[mpu_ns_reg4_of]\n" /* point to mpu_ns.region[4] */\
2488 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2489 "STMIA r0, {r4-r11} \n" /* write MPU_NS RBAR..RLAR_A3 */\
2490 \
2491 /* NS Block 3 (Regions 8-11 / 8 words) */\
2492 "LDR r12, =%[mpu_ns_rnr_idx8] \n" /* r12 = IDX + 8 */\
2493 "STR r12, [r0, #-4] \n" /* MPU_NS->RNR = IDX + 8 */\
2494 "ADD r12, r1, %[mpu_ns_reg8_of]\n" /* point to mpu_ns.region[8] */\
2495 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2496 "STMIA r0, {r4-r11} \n" /* write MPU_NS RBAR..RLAR_A3 */\
2497 \
2498 /* NS Block 4 (Regions 12-15 / 8 words) */\
2499 "LDR r12, =%[mpu_ns_rnr_idx12] \n" /* r12 = IDX + 12 */\
2500 "STR r12, [r0, #-4] \n" /* MPU_NS->RNR = IDX + 12 */\
2501 "ADD r12, r1, %[mpu_ns_reg12_of]\n" /* point to mpu_ns.region[12] */\
2502 "LDMIA r12, {r4-r11} \n" /* burst load 8 words */\
2503 "STMIA r0, {r4-r11} \n" /* write MPU_NS RBAR..RLAR_A3 */\
2504 \
2505 /* Restore NS RNR */\
2506 "LDR r12, =%[mpu_ns_rnr_idx] \n" /* r12 = IDX */\
2507 "STR r12, [r0, #-4] \n" /* restore MPU_NS->RNR */
2508 #else
2509 #error "Unsupported region count, allowed - 2, 4, 6, 8, 12, 16!"
2510 #endif
2511#endif
2512
2513#if STK_CORTEX_M_TRUSTZONE_FRAME
2514 #define STK_ASM_BLOCK_MPU_STACK_GUARD\
2515 "MOV r1, %[st_active] \n" /* r1 = Stack* */\
2516 STK_ASM_BLOCK_MPU_S_STACK_GUARD\
2517 STK_ASM_BLOCK_MPU_NS_STACK_GUARD
2518#else
2519 #define STK_ASM_BLOCK_MPU_STACK_GUARD\
2520 "MOV r1, %[st_active] \n" /* r1 = Stack* */\
2521 STK_ASM_BLOCK_MPU_S_STACK_GUARD
2522#endif
2523
2524extern "C" __stk_attr_naked void STK_PENDSV_HANDLER()
2525{
2526#if (STK_ARCH_CPU_COUNT > 1U)
2527 // optimize register utilization and prevent compiler from using r4-r11
2528 // registers before they are saved. Use r12 IPC scratch register for that
2529 // and calculate offset for p_ctx only once; instruct assembler to use
2530 // r3, r2 for holding pointers to Idle and Active stacks
2531 #ifdef __ICCARM__
2532 register Context *p_ctx = &GetContext();
2533 register Stack *p_st_idle = p_ctx->m_stack_idle;
2534 register Stack *p_st_active = p_ctx->m_stack_active;
2535 #else
2536 register Context *p_ctx __asm("r12") = &GetContext();
2537 register Stack *p_st_idle __asm("r3") = p_ctx->m_stack_idle;
2538 register Stack *p_st_active __asm("r2") = p_ctx->m_stack_active;
2539 #endif
2540#else
2541 #ifdef __ICCARM__
2542 register Stack *p_st_idle = GetContext().m_stack_idle;
2543 register Stack *p_st_active = GetContext().m_stack_active;
2544 #else
2545 register Stack *p_st_idle __asm("r3") = GetContext().m_stack_idle;
2546 register Stack *p_st_active __asm("r2") = GetContext().m_stack_active;
2547 #endif
2548#endif
2549
2550 __asm volatile(
2552
2553 STK_ASM_DISABLE_INTERRUPTS " \n"
2554
2555 // start tracing
2556#if STK_SEGGER_SYSVIEW
2557 STK_SYSVIEW_CALL(StkSystemView_RecordEnterISR)
2558#endif
2559
2560 // save the Secure PSP unconditionally; it is the spine of the entire context
2561 // frame regardless of whether the task was interrupted inside Secure or
2562 // Non-Secure code; PSP_NS, CONTROL_NS, and both PSPLIMs are embedded inside
2563 // the frame (in TrustZoneFrame) so the restore path never needs to branch on
2564 // EXC_RETURN; the CPU's own EXC_RETURN S-bit in the restored LR selects the
2565 // correct return world on "BX LR"
2566 "MRS r0, PSP \n"
2567
2568#if STK_CORTEX_M_FPU
2569 // Save FP registers.
2570 "TST LR, #16 \n" /* test LR for 0xffffffe_, e.g. Thread mode with FP data */
2571
2572 "IT EQ \n" /* if result is positive */
2573 "VMOVEQ.F32 s0, s0 \n" /* force hardware lazy state preservation */
2574
2575 "IT EQ \n" /* if result is positive */
2576 "VSTMDBEQ r0!, {s16-s31} \n" /* store 16 SP registers */
2577#endif
2578
2579 // save registers of inactive task's CPU context:
2580
2581#if STK_CORTEX_M_MANAGE_LR
2582 // save r4-r11 and LR
2583 // note: for Cortex-M3 and higher save LR to keep correct Thread state of the
2584 // task when it is restored
2585 "STMDB r0!, {r4-r11, LR}\n"
2586#else
2587 // note: STMIA is limited to r0-r7 range, therefore save via stack memory
2588 "SUBS r0, r0, #16 \n" /* decrement for r4-r7 */
2589 "STMIA r0!, {r4-r7} \n"
2590 "MOV r4, r8 \n"
2591 "MOV r5, r9 \n"
2592 "MOV r6, r10 \n"
2593 "MOV r7, r11 \n"
2594 "SUBS r0, r0, #32 \n" /* decrement for r8-r11 and pointer reset */
2595 "STMIA r0!, {r4-r7} \n"
2596 "SUBS r0, r0, #16 \n" /* final pointer adjustment */
2597#endif
2598
2599 // ARMv8-M TrustZone: save PSPLIM (Secure), PSPLIM_NS, PSP_NS, and CONTROL_NS
2600 // into the Secure stack frame below the callee-saved registers, matching
2601 // TrustZoneFrame layout; PSP_NS captures the task's Non-Secure stack mid-execution
2602 // (may be inside an NS call that was interrupted); CONTROL_NS preserves NS
2603 // privilege / stack-select
2604#if STK_CORTEX_M_TRUSTZONE_FRAME
2605 // push PSPLIM, PSPLIM_NS (equivalent to STMDB r0!, {PSPLIM, PSPLIM_NS})
2606 "MRS r12, PSPLIM_NS \n"
2607 "STR r12, [r0, #-4]! \n"
2608 "MRS r12, PSPLIM \n"
2609 "STR r12, [r0, #-4]! \n"
2610
2611 // push PSP_NS, CONTROL_NS (equivalent to STMDB r0!, {PSP_NS, CONTROL_NS})
2612 "MRS r12, CONTROL_NS \n"
2613 "STR r12, [r0, #-4]! \n"
2614 "MRS r12, PSP_NS \n"
2615 "STR r12, [r0, #-4]! \n"
2616#endif
2617
2618 // save the outgoing task's live CONTROL register (nPRIV, SPSEL, FPCA, SFPA)
2619 // so that any privilege boost still in effect (SVC_BOOST_PRIV) is preserved verbatim
2620#if STK_CORTEX_M_PRIVILEGE_FRAME
2621 "MRS r12, CONTROL \n"
2622 "STR r12, [r0, #-4]! \n"
2623#endif
2624
2625 // store in GetContext().m_stack_idle
2626 "STR r0, [%[st_idle]] \n" /* store the first member (Stack::SP) from r0 */
2627
2628 // Set per-task MPU guard.
2629#if STK_MPU_STACK_GUARD
2630 STK_ASM_BLOCK_MPU_STACK_GUARD
2631#endif
2632
2633 // load stack of the active task from GetContext().m_stack_active
2634 // note: keep in sync with OnTaskStart
2635 "LDR r0, [%[st_active]]\n" /* load the first member of Stack (Stack::SP) into r0 */
2636
2637 // restore the incoming task's live CONTROL register exactly as it was
2638 // captured when that task was last switched out (or as initialized by
2639 // InitStack for a brand-new task); this is the lowest-addressed word of the
2640 // saved frame (pushed last), so it is popped first, before TrustZoneFrame
2641#if STK_CORTEX_M_PRIVILEGE_FRAME
2642 "LDR r12, [r0], %[access_mode_of] \n"
2643 "MSR CONTROL, r12 \n" /* note: on EXC_RETURN platform flushes instruction cache, therefore we omit explicit ISB */
2644#endif
2645
2646 // ARMv8-M TrustZone: restore TrustZoneFrame fields in reverse push order.
2647 // Pop order: PSP_NS + CONTROL_NS first (pushed last), then PSPLIM + PSPLIM_NS.
2648 // After this, r0 points to the callee-saved register region.
2649 // PSP_S is restored unconditionally after general-register pop.
2650 // CONTROL_NS restores NS privilege/stack-select; PSP_NS restores the task's
2651 // Non-Secure stack to exactly where it was when the task was interrupted.
2652 // EXC_RETURN in LR (restored by LDMIA below) carries the S-bit that tells
2653 // the CPU which world to return to on "BX LR" - no explicit branch needed.
2654#if STK_CORTEX_M_TRUSTZONE_FRAME
2655 "LDR r12, [r0], #4 \n" /* PSP_NS */
2656 "MSR PSP_NS, r12 \n"
2657 "LDR r12, [r0], #4 \n" /* CONTROL_NS */
2658 "MSR CONTROL_NS, r12 \n"
2659
2660 "LDR r12, [r0], #4 \n" /* PSPLIM */
2661 "MSR PSPLIM, r12 \n"
2662 "LDR r12, [r0], #4 \n" /* PSPLIM_NS */
2663 "MSR PSPLIM_NS, r12 \n"
2664#endif
2665
2666 // restore registers of active task's CPU context:
2667
2668#if STK_CORTEX_M_MANAGE_LR
2669 // restore r4-r11 and LR
2670 "LDMIA r0!, {r4-r11, LR}\n"
2671#else
2672 // note: LDMIA is limited to r0-r7 range, therefore load via stack memory
2673 "LDMIA r0!, {r4-r7} \n"
2674 "MOV r8, r4 \n"
2675 "MOV r9, r5 \n"
2676 "MOV r10, r6 \n"
2677 "MOV r11, r7 \n"
2678 "LDMIA r0!, {r4-r7} \n"
2679#endif
2680
2681#if STK_CORTEX_M_FPU
2682 // restore FP registers
2683 "TST LR, #16 \n" /* test LR for 0xffffffe_, e.g. Thread mode with FP data */
2684 "IT EQ \n" /* if result is positive */
2685 "VLDMIAEQ r0!, {s16-s31} \n" /* restore FP registers */
2686#endif
2687
2688 // Restore PSP.
2689 "MSR PSP, r0 \n"
2690
2691 // stop tracing
2692#if STK_SEGGER_SYSVIEW
2693 STK_SYSVIEW_CALL(StkSystemView_OnContextSwitch)
2694#endif
2695
2696 STK_ASM_ENABLE_INTERRUPTS " \n"
2697
2698 STK_ASM_EXIT_FROM_HANDLER " \n"
2699
2700 : /* output: none */
2701 : [access_mode_of] "i" (offsetof(Stack, access_mode))
2702 #ifdef __ICCARM__
2703 , [st_idle] "r3" (p_st_idle)
2704 , [st_active] "r2" (p_st_active)
2705 #else
2706 , [st_idle] "r" (p_st_idle)
2707 , [st_active] "r" (p_st_active)
2708 #endif
2709#if STK_MPU_STACK_GUARD
2710 , [mpu_start_of] "i" (&MPU->RBAR)
2711 , [mpu_reg_of] "i" (offsetof(Stack, mpu.region[0].addr))
2712 #if (STK_MPU_TASK_REGIONS > 4U)
2713 , [mpu_rnr_idx] "i" (STK_CORTEX_M_MPU_TASK_REGION_IDX)
2714 , [mpu_reg4_of] "i" (offsetof(Stack, mpu.region[4].addr))
2715 , [mpu_rnr_idx4] "i" (STK_CORTEX_M_MPU_TASK_REGION_IDX + 4U)
2716 #endif
2717 #if (STK_MPU_TASK_REGIONS > 8U)
2718 , [mpu_reg8_of] "i" (offsetof(Stack, mpu.region[8].addr))
2719 , [mpu_rnr_idx8] "i" (STK_CORTEX_M_MPU_TASK_REGION_IDX + 8U)
2720 #endif
2721 #if (STK_MPU_TASK_REGIONS > 12U)
2722 , [mpu_reg12_of] "i" (offsetof(Stack, mpu.region[12].addr))
2723 , [mpu_rnr_idx12] "i" (STK_CORTEX_M_MPU_TASK_REGION_IDX + 12U)
2724 #endif
2725 #if STK_CORTEX_M_TRUSTZONE_FRAME
2726 , [mpu_ns_start_of] "i" (&MPU_NS->RBAR)
2727 , [mpu_ns_reg_of] "i" (offsetof(Stack, mpu_ns.region[0].addr))
2728 #if (STK_MPU_TASK_REGIONS_NS > 4U)
2729 , [mpu_ns_rnr_idx] "i" (STK_CORTEX_M_MPU_TASK_REGION_IDX_NS)
2730 , [mpu_ns_reg4_of] "i" (offsetof(Stack, mpu_ns.region[4].addr))
2731 , [mpu_ns_rnr_idx4] "i" (STK_CORTEX_M_MPU_TASK_REGION_IDX_NS + 4U)
2732 #endif
2733 #if (STK_MPU_TASK_REGIONS_NS > 8U)
2734 , [mpu_ns_reg8_of] "i" (offsetof(Stack, mpu_ns.region[8].addr))
2735 , [mpu_ns_rnr_idx8] "i" (STK_CORTEX_M_MPU_TASK_REGION_IDX_NS + 8U)
2736 #endif
2737 #if (STK_MPU_TASK_REGIONS_NS > 12U)
2738 , [mpu_ns_reg12_of] "i" (offsetof(Stack, mpu_ns.region[12].addr))
2739 , [mpu_ns_rnr_idx12] "i" (STK_CORTEX_M_MPU_TASK_REGION_IDX_NS + 12U)
2740 #endif
2741 #endif
2742#endif
2743 : "r0" /* used as a scratchpad throughout */
2744#if STK_MPU_STACK_GUARD
2745 , "r1"
2746#endif
2747 , "memory"
2748#if (STK_CORTEX_M_TRUSTZONE_FRAME || STK_MPU_STACK_GUARD || STK_CORTEX_M_PRIVILEGE_FRAME)
2749 , "r12"
2750#endif
2751#if STK_CORTEX_M_FPU
2752 , "cc"
2753#endif
2754 );
2755}
2756
2757// ----------------------------------------------------------------------------
2758namespace {
2759// ----------------------------------------------------------------------------
2760
2761__stk_attr_naked void OnTaskStart()
2762{
2763 // note: HW_DisableInterrupts() must be called prior calling this function
2764
2765#if (STK_ARCH_CPU_COUNT > 1U)
2766 // optimize register utilization and prevent compiler from using r4-r11
2767 // registers before they are saved. Use r12 IPC scratch register for that
2768 // and calculate offset for p_ctx only once; instruct assembler to use
2769 // r2 for holding pointer to the Active stack
2770 #ifdef __ICCARM__
2771 register Context *p_ctx = &GetContext();
2772 register Stack *p_st_active = p_ctx->m_stack_active;
2773 #else
2774 register Context *p_ctx __asm("r12") = &GetContext();
2775 register Stack *p_st_active __asm("r2") = p_ctx->m_stack_active;
2776 #endif
2777#else
2778 #ifdef __ICCARM__
2779 register Stack *p_st_active = GetContext().m_stack_active;
2780 #else
2781 register Stack *p_st_active __asm("r2") = GetContext().m_stack_active;
2782 #endif
2783#endif
2784
2785 __asm volatile(
2787
2788 // set per-task MPU guard
2789#if STK_MPU_STACK_GUARD
2790 STK_ASM_BLOCK_MPU_STACK_GUARD
2791#endif
2792
2793 // load stack of the active task from GetContext().m_stack_active
2794 // note: keep in sync with OnTaskStart
2795 "LDR r0, [%[st_active]]\n" /* load the first member of Stack (Stack::SP) into r0, %[st_active] is a pointer value register */
2796
2797 // restore this task's CONTROL register as initialized by InitStack (see
2798 // PrivilegeFrame); lowest-addressed word of the saved frame, popped first
2799#if STK_CORTEX_M_PRIVILEGE_FRAME
2800 "LDR r12, [r0], %[access_mode_of] \n"
2801 "MSR CONTROL, r12 \n" /* note: on EXC_RETURN platform flushes instruction cache, therefore we omit explicit ISB */
2802#endif
2803
2804 // ARMv8-M TrustZone: restore TrustZoneFrame fields in reverse push order (see STK_PENDSV_HANDLER);
2805 // pop order: PSP_NS + CONTROL_NS first, then PSPLIM + PSPLIM_NS
2806#if STK_CORTEX_M_TRUSTZONE_FRAME
2807 "LDR r12, [r0], #4 \n" /* PSP_NS */
2808 "MSR PSP_NS, r12 \n"
2809 "LDR r12, [r0], #4 \n" /* CONTROL_NS */
2810 "MSR CONTROL_NS, r12 \n"
2811
2812 "LDR r12, [r0], #4 \n" /* PSPLIM */
2813 "MSR PSPLIM, r12 \n"
2814 "LDR r12, [r0], #4 \n" /* PSPLIM_NS */
2815 "MSR PSPLIM_NS, r12 \n"
2816#endif
2817
2818 // restore registers of active task's CPU context:
2819
2820#if STK_CORTEX_M_MANAGE_LR
2821 // restore r4-r11 and LR
2822 "LDMIA r0!, {r4-r11, LR}\n"
2823#else
2824 // note: LDMIA is limited to r0-r7 range, therefore load via stack memory
2825 "LDMIA r0!, {r4-r7} \n"
2826 "MOV r8, r4 \n"
2827 "MOV r9, r5 \n"
2828 "MOV r10, r6 \n"
2829 "MOV r11, r7 \n"
2830 "LDMIA r0!, {r4-r7} \n"
2831#endif
2832
2833#if STK_CORTEX_M_FPU
2834 // restore FP registers
2835 "TST LR, #16 \n" /* test LR for 0xffffffe_, e.g. Thread mode with FP data */
2836 "IT EQ \n" /* if result is positive */
2837 "VLDMIAEQ r0!, {s16-s31} \n" /* restore FP registers */
2838#endif
2839
2840 // restore PSP
2841 "MSR PSP, r0 \n"
2842
2843#if !STK_CORTEX_M_MANAGE_LR
2844 // M0: set LR to Thread mode, use PSP state and stack
2845 "LDR r0, =%[exc_ret] \n"
2846 "MOV LR, r0 \n"
2847#endif
2848
2849 STK_ASM_ENABLE_INTERRUPTS " \n"
2850
2851 STK_ASM_EXIT_FROM_HANDLER " \n"
2852
2853 : /* output: none */
2854 : [access_mode_of] "i" (offsetof(Stack, access_mode))
2855 #ifdef __ICCARM__
2856 , [st_active] "r2" (p_st_active)
2857 #else
2858 , [st_active] "r" (p_st_active)
2859 #endif
2860#if !STK_CORTEX_M_MANAGE_LR
2861 , [exc_ret] "i" (STK_CORTEX_M_EXC_RETURN_THREAD_PSP)
2862#endif
2863#if STK_MPU_STACK_GUARD
2864 , [mpu_start_of] "i" (&MPU->RBAR)
2865 , [mpu_reg_of] "i" (offsetof(Stack, mpu.region[0].addr))
2866 #if (STK_MPU_TASK_REGIONS > 4U)
2867 , [mpu_rnr_idx] "i" (STK_CORTEX_M_MPU_TASK_REGION_IDX)
2868 , [mpu_reg4_of] "i" (offsetof(Stack, mpu.region[4].addr))
2869 , [mpu_rnr_idx4] "i" (STK_CORTEX_M_MPU_TASK_REGION_IDX + 4U)
2870 #endif
2871 #if (STK_MPU_TASK_REGIONS > 8U)
2872 , [mpu_reg8_of] "i" (offsetof(Stack, mpu.region[8].addr))
2873 , [mpu_rnr_idx8] "i" (STK_CORTEX_M_MPU_TASK_REGION_IDX + 8U)
2874 #endif
2875 #if (STK_MPU_TASK_REGIONS > 12U)
2876 , [mpu_reg12_of] "i" (offsetof(Stack, mpu.region[12].addr))
2877 , [mpu_rnr_idx12] "i" (STK_CORTEX_M_MPU_TASK_REGION_IDX + 12U)
2878 #endif
2879 #if STK_CORTEX_M_TRUSTZONE_FRAME
2880 , [mpu_ns_start_of] "i" (&MPU_NS->RBAR)
2881 , [mpu_ns_reg_of] "i" (offsetof(Stack, mpu_ns.region[0].addr))
2882 #if (STK_MPU_TASK_REGIONS_NS > 4U)
2883 , [mpu_ns_rnr_idx] "i" (STK_CORTEX_M_MPU_TASK_REGION_IDX_NS)
2884 , [mpu_ns_reg4_of] "i" (offsetof(Stack, mpu_ns.region[4].addr))
2885 , [mpu_ns_rnr_idx4] "i" (STK_CORTEX_M_MPU_TASK_REGION_IDX_NS + 4U)
2886 #endif
2887 #if (STK_MPU_TASK_REGIONS_NS > 8U)
2888 , [mpu_ns_reg8_of] "i" (offsetof(Stack, mpu_ns.region[8].addr))
2889 , [mpu_ns_rnr_idx8] "i" (STK_CORTEX_M_MPU_TASK_REGION_IDX_NS + 8U)
2890 #endif
2891 #if (STK_MPU_TASK_REGIONS_NS > 12U)
2892 , [mpu_ns_reg12_of] "i" (offsetof(Stack, mpu_ns.region[12].addr))
2893 , [mpu_ns_rnr_idx12] "i" (STK_CORTEX_M_MPU_TASK_REGION_IDX_NS + 12U)
2894 #endif
2895 #endif
2896#endif
2897 : "r0" /* used as a scratchpad throughout */
2898#if STK_MPU_STACK_GUARD
2899 , "r1"
2900#endif
2901 , "memory"
2902#if (STK_CORTEX_M_TRUSTZONE_FRAME || STK_MPU_STACK_GUARD || STK_CORTEX_M_PRIVILEGE_FRAME)
2903 , "r12"
2904#endif
2905#if STK_CORTEX_M_FPU
2906 , "cc"
2907#endif
2908 );
2909}
2910
2911// ----------------------------------------------------------------------------
2912} // namespace
2913// ----------------------------------------------------------------------------
2914
2915void Context::Start()
2916{
2917 m_exiting = false;
2918
2919 // enable FPU before SaveJmp as it references FPU with VMRS
2920 HW_EnableFullFpuAccess();
2921
2922 // save jump location of the Exit trap
2923 STK_UNUSED(drv::SaveJmp(m_exit_buf));
2924 if (m_exiting)
2925 {
2926 // notify kernel about a full stop
2927 m_handler->OnStop();
2928 }
2929 else
2930 {
2931 HW_StartScheduler();
2932 }
2933}
2934
2935void Context::OnStart()
2936{
2937 // interrupts must be disabled at this point
2938 STK_ASSERT(HW_InterruptsDisabled());
2939
2940 // clear FPU usage status if FPU was used before kernel start
2941 HW_ClearFpuState();
2942
2943 // get the first active stack from the kernel
2944 m_handler->OnStart(m_stack_active);
2945
2946 // configure MPU
2947#if STK_MPU
2948 ConfigureMpu();
2949#endif
2950
2951 // set lowest priority for PendSV (SysTick priority is set in StartTickTimer)
2952 NVIC_SetPriority(PendSV_IRQn, STK_CORTEX_M_ISR_PRIORITY_LOWEST);
2953 // set highest priority for SVC interrupts to support critical section for unprivileged tasks
2954#ifdef CONTROL_nPRIV_Msk
2955 NVIC_SetPriority(SVCall_IRQn, STK_CORTEX_M_SVCALL_ISR_PRIORITY);
2956#endif
2957
2958 m_started = true;
2959
2960 // start recording task execution start
2961#if STK_SEGGER_SYSVIEW
2962 SEGGER_SYSVIEW_OnTaskStartExec(m_stack_active->tid);
2963#endif
2964
2965 // start with initially 1 elapsed tick (after timer expires), should be the last
2966 // in OnStart to provide full time window to the first starting task
2967 StartTickTimer(1);
2968}
2969
2970#if STK_TICKLESS_IDLE
2971Timeout Context::Suspend()
2972{
2973 Timeout sleep_ticks;
2974
2975 const uint32_t tick_resolution = GetTickResolutionInClockCycles();
2976 if (tick_resolution != 0U)
2977 {
2978 HW_DisableInterrupts();
2979
2980 // pause SysTick in order to read elapsed value
2981 HW_SysTickDisable();
2982
2983 // get already elapsed CPU cycles since SysTick ISR invocation up to SysTick timer stop (see above)
2984 // to account for them for a new period value
2985 const uint32_t elapsed = HW_SysTickElapsed(HW_SysTickValueAfterDisable());
2986
2987 // stop SysTick timer
2988 HW_SysTickStop();
2989
2990 // clear pending PendSV exception
2991 HW_ClearPendingSwitch();
2992
2993 // notify core about suspension (it will also yield currently active task forcibly)
2994 m_handler->OnSuspend(true);
2995
2996 // update tasks and out currently active task (if any) into a sleep, it will cause a switch
2997 // to a sleep trap after HW_EnableInterrupts, otherwise not
2998 Timeout no_sleep = 0;
2999 OnTick(no_sleep);
3000
3001 // get already elapsed ticks since the OnTick and a call to Suspend(), we shall account for this
3002 // period and return only the remainder
3003 const uint32_t elapsed_ticks = elapsed / tick_resolution;
3004 sleep_ticks = Max(m_sleep_ticks - static_cast<Timeout>(elapsed_ticks), static_cast<Timeout>(0));
3005
3006 HW_EnableInterrupts();
3007 }
3008 else
3009 {
3010 STK_ASSERT(false);
3011 sleep_ticks = NO_WAIT;
3012 }
3013
3014 return sleep_ticks;
3015}
3016#endif // STK_TICKLESS_IDLE
3017
3018#if STK_TICKLESS_IDLE
3019void Context::Resume(Timeout elapsed_ticks)
3020{
3021 HW_DisableInterrupts();
3022
3023 // notify core
3024 m_handler->OnSuspend(false);
3025
3026 // start with initially elapsed ticks (OnTick will fire with elapsed_ticks + 1)
3027 StartTickTimer(elapsed_ticks + 1);
3028
3029 HW_EnableInterrupts();
3030}
3031#endif // STK_TICKLESS_IDLE
3032
3033// __stk_attr_used required for Link-Time Optimization (-flto)
3034extern "C" __stk_attr_used void StkSVCHandlerMain(Word *svc_args)
3035{
3036 // start tracing
3037#if STK_SEGGER_SYSVIEW
3038 SEGGER_SYSVIEW_RecordEnterISR();
3039#endif
3040
3041 // Word is typedef uintptr_t (stk_common.h) - the only integer type the Standard
3042 // blesses for lossless pointer round-trips (MISRA C++ 5-2-8, CERT INT36-C)
3043 STK_STATIC_ASSERT_DESC_N(PTR, sizeof(Word) == sizeof(void *),
3044 "Word must be uintptr_t width for safe pointer round-trip via exc_frame->PC");
3045
3046 // priority 0 (NMI, HardFault) unaffected: SVC (priority 0 per OnStart()) remains
3047 // reachable so SVC_EXIT_CRITICAL can always unwind
3048 STK_STATIC_ASSERT_DESC_N(NVIC, (__NVIC_PRIO_BITS >= 1U) && (__NVIC_PRIO_BITS <= 8U),
3049 "NVIC priority bit width out of range");
3050#ifdef CONTROL_nPRIV_Msk
3051 STK_STATIC_ASSERT_DESC_N(SVC_PRIO, (STK_CORTEX_M_SVCALL_ISR_PRIORITY + 1U) < (1U << __NVIC_PRIO_BITS),
3052 "SVCall priority leaves no lower level to mask: BASEPRI would be 0 (masking disabled)");
3053#endif
3054
3055 // 'volatile': R0 is written back to stacked memory, compiler must not eliminate the store
3056 volatile hw::ExceptionFrame *const exc_frame = reinterpret_cast<volatile hw::ExceptionFrame *>(svc_args);
3057
3058 // details: https://developer.arm.com/documentation/ka004005/latest
3059 // Thumb SVC encoding: [15:8] = 0xDF, [7:0] = imm8
3060 // opcode lives two bytes (one Thumb halfword) before the stacked PC:
3061 const uint8_t *const insn_ptr = hw::WordToPtr<const uint8_t>(exc_frame->PC - 2U);
3062 const ESvcCommandId command = static_cast<ESvcCommandId>(*insn_ptr);
3063
3064 switch (command)
3065 {
3066 case SVC_START_SCHEDULING: {
3067 Context &ctx = GetContext();
3068
3069 // disallow duplicate attempt
3070 STK_ASSERT(!ctx.m_started);
3071 if (!ctx.m_started)
3072 {
3073 // make sure interrupts do not interfere, OnStart expects interrupts disabled
3074 HW_DisableInterrupts();
3075
3076 ctx.OnStart();
3077
3078 // start first task
3079 OnTaskStart();
3080 }
3081 break; }
3082
3083 case SVC_FORCE_SWITCH: {
3084 Context &ctx = GetContext();
3085
3086 STK_ASSERT(ctx.m_started);
3087
3088 ctx.OnForceContextSwitch(exc_frame->R0);
3089 break; }
3090
3091#if STK_MPU
3092 case SVC_BOOST_PRIV: {
3093 // limit access to Privilege escalation to STK_MPU_SHARED_CODE_SECTION functions only
3094 // which are read-only and immutable
3095 if ((exc_frame->PC >= hw::PtrToWord(__stk_mpu_shared_code_start)) &&
3096 (exc_frame->PC <= hw::PtrToWord(__stk_mpu_shared_code_end)))
3097 {
3098 __set_CONTROL(__get_CONTROL() & ~CONTROL_nPRIV_Msk);
3099 }
3100 else
3101 {
3103 }
3104 break; }
3105#endif // STK_MPU
3106
3107#ifdef CONTROL_nPRIV_Msk
3108 case SVC_ENTER_CRITICAL: {
3109 const uint32_t saved_basepri = __get_BASEPRI();
3110 __set_BASEPRI(STK_CORTEX_M_BASEPRI_CS); // mask all ISRs with priority lower than SVCall
3111 __DSB(); // BASEPRI write visible to bus before SVC return
3112 __ISB(); // pipeline flush: mask in effect at first caller instruction
3113
3114 GetContext().OnEnterCriticalSection(saved_basepri);
3115 break; }
3116
3117 case SVC_EXIT_CRITICAL: {
3118 uint32_t saved_basepri;
3119 if (GetContext().OnExitCriticalSection(saved_basepri))
3120 {
3121 __DSB(); // drain pending stores before widening interrupt window
3122 __set_BASEPRI(saved_basepri); // restore saved BASEPRI
3123 __ISB(); // pending interrupts at restored priority may fire now
3124 }
3125 break; }
3126#endif // CONTROL_nPRIV_Msk
3127
3128 default: {
3129 // any SVC number not in ESvcCommandId is a defect, panic unconditionally
3131 break; }
3132 }
3133
3134 // stop tracing
3135#if STK_SEGGER_SYSVIEW
3136 SEGGER_SYSVIEW_RecordExitISR();
3137#endif
3138}
3139
3140// details: "How to Write an SVC Function", https://developer.arm.com/documentation/ka004005/latest
3141extern "C" __stk_attr_naked void STK_SVC_HANDLER()
3142{
3143 __asm volatile(
3145 STK_ASM_GLOBAL_SYMBOL(StkSVCHandlerMain)
3146 STK_ASM_ALIGN_2 // ensure the entry point is aligned
3147
3149
3150 // even on Cortex-M3+, a long jump is safer when using LTO, we load address
3151 // into register to allow far jump (>2KB)
3152 "LDR r1, =StkSVCHandlerMain \n"
3153 "BX r1 \n"
3154
3155 STK_ASM_ALIGN_2 // ensure literal pool is aligned
3156 STK_ASM_POOL // ensure literal pool is reachable
3157 );
3158}
3159
3160#if (STK_USE_MEMMANAGE_HANDLER && defined(STK_MEMMANAGE_HANDLER)) ||\
3161 (STK_USE_HARDFAULT_HANDLER && defined(STK_HARDFAULT_HANDLER))
3162extern "C" __stk_attr_used
3163void StkExceptionHandlerMain(const Word *stacked_regs, Word exc_id)
3164{
3165 Word exc_return;
3166 __asm volatile ("MOV %0, LR" : "=r" (exc_return) :: "memory"); // EXC_RETURN, if not already clobbered
3167
3168 bool handled = false;
3169 Context &ctx = GetContext();
3170
3171 if (ctx.m_overrider != nullptr)
3172 {
3173 static FaultContext fault_ctx;
3174 fault_ctx.Fill(stacked_regs, exc_return);
3175
3176 handled = ctx.m_overrider->OnException(static_cast<EHwException>(exc_id), ctx.GetCurrentTId(),
3177 &fault_ctx);
3178 }
3179
3180 if (!handled)
3181 {
3182 // default handler of memory management exception:
3183
3184 // 1. break in Debugger
3186
3187 // 2. restart device
3188 NVIC_SystemReset();
3189 }
3190}
3191#endif // STK_USE_MEMMANAGE_HANDLER || STK_USE_HARDFAULT_HANDLER
3192
3193#if STK_USE_MEMMANAGE_HANDLER && defined(STK_MEMMANAGE_HANDLER)
3194extern "C" __stk_attr_naked void STK_MEMMANAGE_HANDLER()
3195{
3196 __asm volatile(
3198 STK_ASM_GLOBAL_SYMBOL(StkExceptionHandlerMain)
3200
3202
3203 "MOVS r1, %0 \n"
3204 "LDR r2, =StkExceptionHandlerMain \n"
3205 "BX r2 \n"
3206
3209 : /* no output */
3210 : "i" (HW_EXCEPT_MEMACCESS)
3211 : /* no clobber */
3212 );
3213}
3214#endif // STK_USE_MEMMANAGE_HANDLER
3215
3216#if STK_USE_HARDFAULT_HANDLER && defined(STK_HARDFAULT_HANDLER)
3217extern "C" __stk_attr_naked void STK_HARDFAULT_HANDLER()
3218{
3219 __asm volatile(
3221 STK_ASM_GLOBAL_SYMBOL(StkExceptionHandlerMain)
3223
3225
3226 "MOVS r1, %0 \n"
3227 "LDR r2, =StkExceptionHandlerMain \n"
3228 "BX r2 \n"
3229
3232 : /* no output */
3233 : "i" (HW_EXCEPT_FATAL)
3234 : /* no clobber */
3235 );
3236}
3237#endif // STK_USE_HARDFAULT_HANDLER
3238
3240void drv::OnTaskRun(ITask *runnable)
3241{
3242 STK_ASSERT(runnable != nullptr);
3243 runnable->Run();
3244}
3245
3247void drv::OnTaskExit()
3248{
3249 const Context &ctx = GetContext();
3250
3251 const uint32_t cs = HW_CriticalSectionStart();
3252
3253 ctx.m_handler->OnTaskExit(ctx.m_stack_active);
3254
3255 HW_CriticalSectionEnd(cs);
3256
3257 if (HW_IsPrivilegedContext())
3258 {
3259 for (;;)
3260 {
3261 // enter standby mode until time slot expires
3262 HW_EnterSleepMode();
3263 }
3264 }
3265 else
3266 {
3267 for (;;)
3268 {
3269 // can only busy-wait when non-Privileged
3270 __stk_relax_cpu();
3271 }
3272 }
3273}
3274
3276void drv::OnSchedulerSleep()
3277{
3278 // if hit here, increase the size of STK_SLEEP_TRAP_STACK_SIZE
3280
3281 for (;;)
3282 {
3283 // record Idle state
3284 #if STK_SEGGER_SYSVIEW
3285 SEGGER_SYSVIEW_OnIdle();
3286 #endif
3287
3288 HW_EnterSleepMode();
3289 }
3290}
3291
3293void drv::OnSchedulerSleepOverride()
3294{
3295 // if hit here, increase the size of STK_SLEEP_TRAP_STACK_SIZE
3297
3298 for (;;)
3299 {
3300 // record Idle state
3301 #if STK_SEGGER_SYSVIEW
3302 SEGGER_SYSVIEW_OnIdle();
3303 #endif
3304
3305 GetContext().OnSleepOverride();
3306 }
3307}
3308
3310void drv::OnSchedulerExit()
3311{
3312 __set_CONTROL(0U); // switch to MSP
3313 __set_PSP(0U); // clear PSP (for a clean register state)
3314
3315 // jump back to SaveJmp's return site with m_exiting already set to true
3316 drv::RestoreJmp(GetContext().m_exit_buf, 0);
3317}
3318
3319void PlatformArmCortexM::Initialize(IEventHandler *event_handler, IKernelService *service, uint32_t resolution_us,
3320 Stack *exit_trap)
3321{
3322 GetContext().Initialize(event_handler, service, exit_trap, resolution_us);
3323}
3324
3326{
3327 GetContext().Start();
3328}
3329
3330#if STK_MPU_STACK_GUARD
3331// ----------------------------------------------------------------------------
3332namespace {
3333// ----------------------------------------------------------------------------
3334
3335void ConfigureTaskMpu(IPlatform::IEventOverrider *overrider, Stack *stack, IStackMemory *stack_memory,
3336 ITask *user_task, bool non_secure)
3337{
3338#if !STK_CORTEX_M_TRUSTZONE_FRAME
3339 STK_UNUSED(non_secure);
3340#endif
3341
3342 MpuRegionConfig task_mpu_cfg[TaskMpu::NUM_REGIONS];
3343 size_t cfg_count = 0U;
3344
3345 // merge in up to (TaskMpu::NUM_REGIONS - 1) application-defined regions
3346 // (task-relative slots [+0..+NUM_REGIONS-2)]; user_task is null for the internal
3347 // sleep/exit trap stacks, which never carry application-defined regions
3348 if (user_task != nullptr)
3349 {
3350 // note: if task does not provide MPU regions the stack guard is not applied either
3351 // this approach avoids stack guard for Privileged tasks with MPU=MPU_CFG_PRIVILEGED_BG_MEM
3352 const MpuRegionList *const regions = user_task->GetMpuRegions();
3353 if (regions != nullptr)
3354 {
3355 // Prepare the stack guard bounds for the overlap check
3356 const MpuRegionConfig stack_guard_cfg =
3357 {
3358 .addr = hw::PtrToWord(stack_memory->GetStack()),
3359 .size = stack_memory->GetStackSize() * sizeof(Word),
3360 .access_perm = hw::mpu::EMpuAccess::ACCESS_FULL,
3361 .mem_type = hw::mpu::EMpuType::TYPE_NORMAL_CACHEABLE,
3362 .share = hw::mpu::EMpuShare::SHARE_NON,
3363 .exec = hw::mpu::EMpuExec::EXEC_NEVER
3364 };
3365
3366 const hw_int::pmsa::RegionBounds bounds_s = hw_int::pmsa::GetRegionBounds(stack_guard_cfg);
3367
3368 size_t regions_count = regions->GetSize();
3369 bool stack_guard_canceled = false;
3370
3371 for (size_t i = 0U; i < regions_count; ++i)
3372 {
3373 const hw_int::pmsa::RegionBounds bounds_i = hw_int::pmsa::GetRegionBounds((*regions)[i]);
3374
3375 // cancel stack region in favour of memory coverage by task's instance
3376 if (hw_int::pmsa::IsOverlapping(bounds_s, bounds_i))
3377 {
3378 stack_guard_canceled = true;
3379 break;
3380 }
3381 }
3382
3383 // Reserve 1 slot for the stack guard at the end unless canceled
3384 const size_t max_user_regions = stack_guard_canceled ? TaskMpu::NUM_REGIONS : (TaskMpu::NUM_REGIONS - 1U);
3385
3386 STK_ASSERT(regions_count <= max_user_regions);
3387 if (regions_count > max_user_regions)
3388 {
3389 regions_count = max_user_regions; // defensively clamp in release builds
3390 }
3391
3392 // Populate initial slots with user-defined regions
3393 for (size_t i = 0U; i < regions_count; ++i)
3394 {
3395 task_mpu_cfg[cfg_count++] = (*regions)[i];
3396 }
3397
3398 // Append stack guard as the final slot if not canceled by an overlap
3399 if (!stack_guard_canceled)
3400 {
3401 task_mpu_cfg[cfg_count++] = stack_guard_cfg;
3402 }
3403 }
3404 }
3405
3406#if STK_CORTEX_M_TRUSTZONE_FRAME
3407 TaskMpu &task_mpu = (non_secure ? stack->mpu_ns : stack->mpu);
3408#else
3409 TaskMpu &task_mpu = stack->mpu;
3410#endif
3411
3412 // any slots beyond cfg_count are left disabled by ConfigureDynamic() itself
3413 hw_int::pmsa::ConfigureDynamic(task_mpu, task_mpu_cfg, cfg_count, overrider, non_secure);
3414}
3415
3416// ----------------------------------------------------------------------------
3417} // namespace
3418// ----------------------------------------------------------------------------
3419#endif // STK_MPU_STACK_GUARD
3420
3421#if STK_CORTEX_M_TRUSTZONE_FRAME
3422// ----------------------------------------------------------------------------
3423namespace {
3424// ----------------------------------------------------------------------------
3425
3426void ConfigureTaskTrustZone(Stack *stack, IStackMemory *stack_memory, ITask *user_task, bool non_secure)
3427{
3428 // write TrustZoneFrame immediately above stack->SP (below the r4-r11 region);
3429 // frame layout in memory (low -> high address, matching STMDB push order in PendSV):
3430 // [SP+0] PSP_NS (pushed last by 2nd STMDB, popped first by 1st LDMIA)
3431 // [SP+4] CONTROL_NS
3432 // [SP+8] PSPLIM (pushed first by 1st STMDB, popped last by 2nd LDMIA)
3433 // [SP+12] PSPLIM_NS
3434 // TrustZoneFrame sits one word above Stack::SP when PrivilegeFrame is present,
3435 // since PrivilegeFrame::CONTROL occupies the lowest word of the saved region
3436 TrustZoneFrame *const tz_frame = hw::WordToPtr<TrustZoneFrame>(stack->SP + (STK_CORTEX_M_PRIV_REGISTER_COUNT * sizeof(Word)));
3437 tz_frame->PSP_NS = 0U;
3438 tz_frame->CONTROL_NS = hw_int::reg::CONTROL::DEFAULT_INIT;
3439 tz_frame->PSPLIM = 0U; // unlimited for Secure task
3440 tz_frame->PSPLIM_NS = 0U; // unlimited for Secure task
3441
3442 if (non_secure)
3443 {
3444 // instance points to non-secure stack memory, secure memory is obtained by GetSecureStackMemory
3445 IStackMemory *ns_stack_memory = user_task;
3446
3447 // initialize stack memory
3448 const Word ns_stack_top = Context::InitStackMemory(ns_stack_memory);
3449
3450 // NS thread Privilege status
3451 tz_frame->CONTROL_NS = (((stack->access_mode & ACCESS_PRIVILEGED) != 0U) ?
3452 hw_int::reg::CONTROL::SetPrivileged(tz_frame->CONTROL_NS) :
3453 hw_int::reg::CONTROL::SetUnprivileged(tz_frame->CONTROL_NS));
3454
3455 // NS thread is using PSP_NS
3456 tz_frame->CONTROL_NS = hw_int::reg::CONTROL::SetSPSelectionToPSP(tz_frame->CONTROL_NS);
3457
3458 // for a fresh NS task, PSP_NS starts at the top of its NS stack
3459 // (the hardware exception frame was placed there by the caller)
3460 tz_frame->PSP_NS = ns_stack_top;
3461
3462 // bottom of NS stack
3463 tz_frame->PSPLIM_NS = hw::PtrToWord(ns_stack_memory->GetStack());
3464
3465 // bottom of S stack
3466 tz_frame->PSPLIM = hw::PtrToWord(stack_memory->GetStack());
3467 }
3468 else
3469 {
3470 stack->access_mode |= ACCESS_SECURE;
3471
3472 // bottom of S stack
3473 tz_frame->PSPLIM = hw::PtrToWord(stack_memory->GetStack());
3474 }
3475}
3476
3477// ----------------------------------------------------------------------------
3478} // namespace
3479// ----------------------------------------------------------------------------
3480#endif // STK_CORTEX_M_TRUSTZONE_FRAME
3481
3482void PlatformArmCortexM::InitStack(EStackType stack_type, Stack *stack, IStackMemory *stack_memory, ITask *user_task)
3483{
3485 "ExceptionFrame layout must match the ARMv7-M hardware exception frame exactly");
3486 STK_ASSERT(stack_memory->GetStackSize() > STK_CORTEX_M_TOTAL_REGISTER_COUNT);
3487
3488 const Context &ctx = GetContext();
3489
3490#ifdef _STK_CORTEX_M_TRUSTZONE
3491 bool is_non_secure_task = false;
3492
3493 // Replace stack_memory of Non-Secure task with a Secure memory which is required for launching the task
3494 // and Secure operations via NSC veneer.
3495 if ((user_task != nullptr) && (user_task->GetSecureStackMemory() != nullptr))
3496 {
3497 stack_memory = user_task->GetSecureStackMemory();
3498 is_non_secure_task = true;
3499
3500 STK_ASSERT(stack_memory->GetStackSize() > STK_CORTEX_M_TOTAL_REGISTER_COUNT);
3501 }
3502#endif
3503
3504 // initialize stack memory
3505 const Word stack_top = Context::InitStackMemory(stack_memory);
3506
3507 // initialize Stack Pointer (SP)
3508 stack->SP = stack_top - (STK_CORTEX_M_TOTAL_REGISTER_COUNT * sizeof(Word));
3509
3510 // place the initial task frame flush against the top of the stack:
3511 // TaskFrame::exc (ExceptionFrame) occupies the top 8 words, TaskFrame::EXC_RETURN
3512 // (when present) sits immediately below it, and TrustZoneFrame (when present) sits
3513 // below that
3514 TaskFrame *const task_frame = hw::WordToPtr<TaskFrame>(stack_top - sizeof(TaskFrame));
3515
3516 // initialize registers for the user task's first start
3517 switch (stack_type)
3518 {
3519 case STACK_USER_TASK: {
3520 task_frame->exc.PC = hw::PtrToWord(&drv::OnTaskRun);
3521 task_frame->exc.LR = hw::PtrToWord(&drv::OnTaskExit);
3522 task_frame->exc.R0 = hw::PtrToWord(user_task);
3523 break; }
3524
3525 case STACK_SLEEP_TRAP: {
3526 task_frame->exc.PC = hw::PtrToWord(ctx.m_overrider != nullptr ? &drv::OnSchedulerSleepOverride : &drv::OnSchedulerSleep);
3527 task_frame->exc.LR = STK_STACK_MEMORY_FILLER; // should not attempt to exit
3528 task_frame->exc.R0 = 0U;
3529 break; }
3530
3531 case STACK_EXIT_TRAP: {
3532 task_frame->exc.PC = hw::PtrToWord(&drv::OnSchedulerExit);
3533 task_frame->exc.LR = STK_STACK_MEMORY_FILLER; // should not attempt to exit
3534 task_frame->exc.R0 = 0U;
3535 break; }
3536
3537 default: {
3539 break; }
3540 }
3541
3542 // ensure the Program Counter is properly aligned to halfword boundaries
3543 // by clearing the Thumb state tracking bit from the physical target address
3544 task_frame->exc.PC = hw_int::reg::PC::ClearThumbBit(task_frame->exc.PC);
3545
3546 // initialize the Execution Program Status Register (EPSR) with the T-bit enabled,
3547 // which is required for all ARM Cortex-M processors to execute instructions
3548 task_frame->exc.xPSR = hw_int::reg::XPSR::SetThumbExecution(hw_int::reg::XPSR::DEFAULT_INIT);
3549
3550#if STK_CORTEX_M_MANAGE_LR
3551 // set the EXC_RETURN value to target Thread Mode using the Process Stack Pointer (PSP)
3552 // Note for TrustZone configurations: Execution will return to the Non-Secure state
3553 // via a Secure-side trampoline, which invokes the task's entry/ITask::Run function
3554 // using a dedicated Non-Secure function call (__BXNS / Non-Secure callable boundary)
3555 task_frame->EXC_RETURN = STK_CORTEX_M_EXC_RETURN_THREAD_PSP;
3556#endif // STK_CORTEX_M_MANAGE_LR
3557
3558#if STK_CORTEX_M_PRIVILEGE_FRAME
3559 // initial CONTROL for this task's first start: nPRIV reflects the task's
3560 // static access_mode. SPSEL is fixed up by hardware from EXC_RETURN on the
3561 // first exception return; FPCA/SFPA start clear. This word sits at the very
3562 // bottom of the saved frame (== stack->SP) and is popped first by
3563 // OnTaskStart/STK_PENDSV_HANDLER (see PrivilegeFrame)
3564 {
3565 PrivilegeFrame *const priv_frame = hw::WordToPtr<PrivilegeFrame>(stack->SP);
3566
3567 priv_frame->CONTROL = (((stack->access_mode & ACCESS_PRIVILEGED) != 0U) ?
3568 hw_int::reg::CONTROL::SetPrivileged(hw_int::reg::CONTROL::DEFAULT_INIT) :
3569 hw_int::reg::CONTROL::SetUnprivileged(hw_int::reg::CONTROL::DEFAULT_INIT));
3570 }
3571#endif
3572
3573 // configure per-task TrustZone hardware frames and boundaries
3574#if STK_CORTEX_M_TRUSTZONE_FRAME
3575 ConfigureTaskTrustZone(stack, stack_memory, user_task, is_non_secure_task);
3576#endif
3577
3578 // configure per-task MPU
3579#if STK_MPU_STACK_GUARD
3580 ConfigureTaskMpu(ctx.m_overrider, stack, stack_memory, user_task, false);
3581 #if STK_CORTEX_M_TRUSTZONE_FRAME
3582 if (is_non_secure_task)
3583 {
3584 ConfigureTaskMpu(ctx.m_overrider_ns, stack, user_task, user_task, true);
3585 }
3586 #endif
3587#endif
3588}
3589
3590// ---------------------------------------------------------------------------
3591// ARMv8-M TrustZone Non-Secure callable (NSC) gateway veneers.
3592// ---------------------------------------------------------------------------
3593
3594// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3595#ifdef _STK_CORTEX_M_TRUSTZONE
3596// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3597
3606__stk_forceinline int NSC_GetCallerAccessFlags()
3607{
3608 int flags = (CMSE_NONSECURE | CMSE_MPU_READWRITE);
3609
3610 if ((HW_GetCurrentException() == 0U) && ((__TZ_get_CONTROL_NS() & CONTROL_nPRIV_Msk) != 0U))
3611 {
3612 flags |= CMSE_MPU_UNPRIV;
3613 }
3614
3615 return flags;
3616}
3617
3621hw::CriticalSection::Session NSC_stk_hw_CriticalSection_Enter(const hw::CriticalSection::Session ses)
3622{
3623 return hw::CriticalSection::Enter(ses);
3624}
3625
3629void NSC_stk_hw_CriticalSection_Exit(const hw::CriticalSection::Session ses)
3630{
3632}
3633
3637void NSC_stk_hw_SpinLock_Lock(hw::SpinLock *sl)
3638{
3639 if ((sl != nullptr) && (cmse_check_pointed_object(sl, NSC_GetCallerAccessFlags()) != nullptr))
3640 {
3641 sl->Lock();
3642 }
3643}
3644
3648void NSC_stk_hw_SpinLock_Unlock(hw::SpinLock *sl)
3649{
3650 if ((sl != nullptr) && (cmse_check_pointed_object(sl, NSC_GetCallerAccessFlags()) != nullptr))
3651 {
3652 sl->Unlock();
3653 }
3654}
3655
3659bool NSC_stk_hw_SpinLock_TryLock(hw::SpinLock *sl)
3660{
3661 bool locked;
3662
3663 if ((sl != nullptr) && (cmse_check_pointed_object(sl, NSC_GetCallerAccessFlags()) != nullptr))
3664 {
3665 locked = sl->TryLock();
3666 }
3667 else
3668 {
3669 locked = false;
3670 }
3671
3672 return locked;
3673}
3674
3679void NSC_stk_hw_HiResClock_Init(hw::HiResClock *clk)
3680{
3681 if ((clk != nullptr) && (cmse_check_pointed_object(clk, NSC_GetCallerAccessFlags()) != nullptr))
3682 {
3683 (*clk) = hw::HiResClock();
3684 }
3685}
3686
3691uint32_t NSC_stk_hw_HiResClock_GetFrequency()
3692{
3693 return HW_CoreClockFrequency();
3694}
3695
3701stk::Cycles NSC_stk_hw_HiResClock_GetCycles(hw::HiResClock *clk)
3702{
3703 stk::Cycles cycles = 0U;
3704
3705 if ((clk != nullptr) && (cmse_check_pointed_object(clk, NSC_GetCallerAccessFlags()) != nullptr))
3706 {
3707 cycles = clk->GetCycles();
3708 }
3709
3710 return cycles;
3711}
3712
3716bool NSC_stk_hw_IsInsideISR()
3717{
3718 return hw::IsInsideISR();
3719}
3720
3724TId NSC_stk_debug_GetCurrentTId()
3725{
3726 return GetContext().GetCurrentTId();
3727}
3728
3729// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3730#endif // _STK_CORTEX_M_TRUSTZONE
3731// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3732
3733void Context::OnStop()
3734{
3735#if STK_SEGGER_SYSVIEW
3736 SEGGER_SYSVIEW_Stop();
3737#endif
3738
3739 // stop SysTick timer
3740 HW_SysTickStop();
3741
3742 // clear pending PendSV exception
3743 HW_ClearPendingSwitch();
3744
3745 m_started = false;
3746 m_exiting = true;
3747
3748 // make sure all assignments are set and executed
3749 __DSB();
3750 __ISB();
3751}
3752
3754{
3755 GetContext().OnStop();
3756
3757 // load context of the Exit trap
3758 HW_DisableInterrupts();
3759 OnTaskStart();
3760}
3761
3763{
3764 return GetContext().m_tick_resolution;
3765}
3766
3768{
3769 return static_cast<Cycles>(HW_SysTickValue());
3770}
3771
3773{
3774 return HW_CoreClockFrequency();
3775}
3776
3778{
3779 HW_ForceContextSwitch(id);
3780}
3781
3783{
3784 TId result;
3785 const Word isr = HW_GetCurrentException();
3786
3787 // return special TId which denotes ISR
3788 if (isr != 0U)
3789 {
3790 const TId isr_tid = (TID_ISR_N | isr);
3791 STK_ASSERT(IsIsrTid(isr_tid));
3792 result = isr_tid;
3793 }
3794 else
3795 {
3796 const Context &ctx = GetContext();
3797
3798 if (ctx.m_started)
3799 {
3800 result = ctx.m_handler->OnGetTid(HW_GetCallerSP());
3801 }
3802 else
3803 {
3804 result = TID_NONE;
3805 }
3806 }
3807
3808 return result;
3809}
3810
3812{
3813 bool is_handled = false;
3814 const Context &ctx = GetContext();
3815
3816 if (ctx.m_overrider != nullptr)
3817 {
3818 is_handled = ctx.m_overrider->OnHardFault();
3819 }
3820
3821 if (!is_handled)
3822 {
3824 }
3825}
3826
3827void PlatformArmCortexM::SetEventOverrider(IEventOverrider *overrider, bool non_secure)
3828{
3829 Context &ctx = GetContext();
3830
3831 STK_ASSERT(!ctx.m_started);
3832
3833#if STK_CORTEX_M_TRUSTZONE_FRAME
3834 if (non_secure)
3835 {
3836 ctx.m_overrider_ns = overrider;
3837 }
3838 else
3839#else
3840 STK_UNUSED(non_secure);
3841#endif
3842 {
3843 ctx.m_overrider = overrider;
3844 }
3845}
3846
3848{
3849 return HW_GetCallerSP();
3850}
3851
3853{
3854#if STK_TICKLESS_IDLE
3855 return GetContext().Suspend();
3856#else
3857 return 0;
3858#endif
3859}
3860
3861void PlatformArmCortexM::Resume(Timeout elapsed_ticks)
3862{
3863#if STK_TICKLESS_IDLE
3864 GetContext().Resume(elapsed_ticks);
3865#else
3866 STK_UNUSED(elapsed_ticks);
3867#endif
3868}
3869
3870void PlatformArmCortexM::SetCpuFrequency(uint8_t core_id, uint32_t frequency)
3871{
3873
3874 if (core_id == 0xFFU)
3875 {
3876 for (uint8_t i = 0U; i < STK_STATIC_ARRAY_SIZE(s_StkSystemCoreClock); ++i)
3877 {
3878 s_StkSystemCoreClock[i] = frequency;
3879 }
3880 }
3881 else
3882 {
3883 STK_ASSERT(core_id < STK_STATIC_ARRAY_SIZE(s_StkSystemCoreClock));
3884
3885 if (core_id < STK_STATIC_ARRAY_SIZE(s_StkSystemCoreClock))
3886 {
3887 s_StkSystemCoreClock[core_id] = frequency;
3888 }
3889 }
3890}
3891
3892// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3893#if STK_MPU
3894// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3895
3896// ----------------------------------------------------------------------------
3897namespace {
3898// ----------------------------------------------------------------------------
3899
3912class KernelServiceSvcProxy final : public stk::IKernelService
3913{
3914public:
3917 explicit KernelServiceSvcProxy() : IKernelService()
3918 {}
3919
3923 STK_VIRT_DTOR ~KernelServiceSvcProxy() = default;
3924
3925 stk::TId GetTid() const override;
3926 stk::Ticks GetTicks() const override;
3927 uint32_t GetTickResolution() const override;
3928 stk::Cycles GetSysTimerCount() const override;
3929 uint32_t GetSysTimerFrequency() const override;
3930 void Delay(stk::Timeout ticks) override;
3931 bool Sleep(stk::Timeout ticks) override;
3932 bool SleepUntil(stk::Ticks timestamp) override;
3933 void SleepCancel(stk::TId task_id) override;
3934 void SwitchToNext() override;
3935 stk::EWaitResult Wait(stk::ISyncObject *sobj, stk::IMutex *mutex, stk::Timeout timeout) override;
3936 void Wake(stk::ISyncObject *sobj, bool all);
3937 stk::Timeout Suspend() override;
3938 void Resume(stk::Timeout elapsed_ticks) override;
3939 void InheritWeight(stk::TId tid, stk::Weight weight) override;
3940 void RestoreWeight(stk::TId tid, stk::ISyncObject *sobj) override;
3941 stk::Weight SetWeight(stk::TId tid, stk::Weight weight) override;
3942 stk::Timeout SetTimeSlice(stk::TId tid, stk::Timeout ticks) override;
3943 void ExitTask() override;
3944 ELockResult LockScheduler() override;
3945 ELockResult UnlockScheduler() override;
3946 ELockResult RestoreSchedulerLock(bool locked) override;
3947};
3948
3953KernelServiceSvcProxy s_StkKernelServiceUnprivProxy;
3954
3955// ----------------------------------------------------------------------------
3956} // namespace
3957// ----------------------------------------------------------------------------
3958
3960stk::TId KernelServiceSvcProxy::GetTid() const
3961{
3962 const mpu::ScopedPrivilegeBoost pb;
3963 return GetContext().m_service->GetTid();
3964}
3965
3967stk::Ticks KernelServiceSvcProxy::GetTicks() const
3968{
3969 const mpu::ScopedPrivilegeBoost pb;
3970 return GetContext().m_service->GetTicks();
3971}
3972
3974uint32_t KernelServiceSvcProxy::GetTickResolution() const
3975{
3976 const mpu::ScopedPrivilegeBoost pb;
3977 return GetContext().m_tick_resolution;
3978}
3979
3981stk::Cycles KernelServiceSvcProxy::GetSysTimerCount() const
3982{
3983 const mpu::ScopedPrivilegeBoost pb;
3984 return GetContext().m_service->GetSysTimerCount();
3985}
3986
3988uint32_t KernelServiceSvcProxy::GetSysTimerFrequency() const
3989{
3990 const mpu::ScopedPrivilegeBoost pb;
3991 return GetContext().m_service->GetSysTimerFrequency();
3992}
3993
3995void KernelServiceSvcProxy::Delay(stk::Timeout ticks)
3996{
3997 const mpu::ScopedPrivilegeBoost pb;
3998 GetContext().m_service->Delay(ticks);
3999}
4000
4002bool KernelServiceSvcProxy::Sleep(stk::Timeout ticks)
4003{
4004 const mpu::ScopedPrivilegeBoost pb;
4005 return GetContext().m_service->Sleep(ticks);
4006}
4007
4009bool KernelServiceSvcProxy::SleepUntil(stk::Ticks timestamp)
4010{
4011 const mpu::ScopedPrivilegeBoost pb;
4012 return GetContext().m_service->SleepUntil(timestamp);
4013}
4014
4016void KernelServiceSvcProxy::SleepCancel(stk::TId task_id)
4017{
4018 const mpu::ScopedPrivilegeBoost pb;
4019 GetContext().m_service->SleepCancel(task_id);
4020}
4021
4023void KernelServiceSvcProxy::SwitchToNext()
4024{
4025 const mpu::ScopedPrivilegeBoost pb;
4026 GetContext().m_service->SwitchToNext();
4027}
4028
4030void KernelServiceSvcProxy::ExitTask()
4031{
4032 const mpu::ScopedPrivilegeBoost pb;
4033 GetContext().m_service->ExitTask();
4034}
4035
4037ELockResult KernelServiceSvcProxy::LockScheduler()
4038{
4039 const mpu::ScopedPrivilegeBoost pb;
4040 return GetContext().m_service->LockScheduler();
4041}
4042
4044ELockResult KernelServiceSvcProxy::UnlockScheduler()
4045{
4046 const mpu::ScopedPrivilegeBoost pb;
4047 return GetContext().m_service->UnlockScheduler();
4048}
4049
4051ELockResult KernelServiceSvcProxy::RestoreSchedulerLock(bool locked)
4052{
4053 const mpu::ScopedPrivilegeBoost pb;
4054 return GetContext().m_service->RestoreSchedulerLock(locked);
4055}
4056
4058stk::EWaitResult KernelServiceSvcProxy::Wait(stk::ISyncObject *sobj, stk::IMutex *mutex, stk::Timeout timeout)
4059{
4060 const mpu::ScopedPrivilegeBoost pb;
4061 return GetContext().m_service->Wait(sobj, mutex, timeout);
4062}
4063
4065void KernelServiceSvcProxy::Wake(stk::ISyncObject *sobj, bool all)
4066{
4067 const mpu::ScopedPrivilegeBoost pb;
4068 GetContext().m_service->Wake(sobj, all);
4069}
4070
4072stk::Timeout KernelServiceSvcProxy::Suspend()
4073{
4074 return 0; // not allowed for non-privileged process
4075}
4076
4078void KernelServiceSvcProxy::Resume(stk::Timeout elapsed_ticks)
4079{
4080 // not allowed for non-privileged process
4081 STK_UNUSED(elapsed_ticks);
4082}
4083
4085void KernelServiceSvcProxy::InheritWeight(stk::TId tid, stk::Weight weight)
4086{
4087 const mpu::ScopedPrivilegeBoost pb;
4088 GetContext().m_service->InheritWeight(tid, weight);
4089}
4090
4092void KernelServiceSvcProxy::RestoreWeight(stk::TId tid, stk::ISyncObject *sobj)
4093{
4094 const mpu::ScopedPrivilegeBoost pb;
4095 GetContext().m_service->RestoreWeight(tid, sobj);
4096}
4097
4099stk::Weight KernelServiceSvcProxy::SetWeight(stk::TId tid, stk::Weight weight)
4100{
4101 const mpu::ScopedPrivilegeBoost pb;
4102 return GetContext().m_service->SetWeight(tid, weight);
4103}
4104
4106stk::Timeout KernelServiceSvcProxy::SetTimeSlice(stk::TId tid, stk::Timeout ticks)
4107{
4108 const mpu::ScopedPrivilegeBoost pb;
4109 return GetContext().m_service->SetTimeSlice(tid, ticks);
4110}
4111
4112// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4113#endif // STK_MPU
4114// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4115
4117{
4118 IKernelService *service;
4119
4120#if STK_MPU
4121 if (HW_IsPrivilegedContext() || HW_IsHandlerMode())
4122 {
4123 service = GetContext().m_service;
4124 }
4125 else
4126 {
4127 service = &s_StkKernelServiceUnprivProxy;
4128 }
4129#else
4130 service = GetContext().m_service;
4131#endif
4132
4133 return service;
4134}
4135
4138{
4140
4141 const bool is_priv_ses = ((ses & SESSION_FLAG_NPRIV) == 0U);
4142 const bool is_priv_ctx = HW_IsPrivilegedContext();
4143 const bool is_handler = HW_IsHandlerMode();
4144
4145 if (is_priv_ses && (is_priv_ctx || is_handler))
4146 {
4147 GetContext().EnterCriticalSection();
4148 ret = SESSION_FLAG_NONE;
4149 }
4150 else
4151 {
4152 HW_UnprivEnterCriticalSection();
4153 ret = SESSION_FLAG_NPRIV;
4154 }
4155
4156 return ret;
4157}
4158
4161{
4162 if ((ses & SESSION_FLAG_NPRIV) == 0U)
4163 {
4164 GetContext().ExitCriticalSection();
4165 }
4166 else
4167 {
4168 HW_UnprivExitCriticalSection();
4169 }
4170}
4171
4174{
4175 HW_SpinLockLock(m_lock);
4176}
4177
4180{
4181 HW_SpinLockUnlock(m_lock);
4182}
4183
4186{
4187 return HW_SpinLockTryLock(m_lock);
4188}
4189
4192{
4193 return HW_IsHandlerMode();
4194}
4195
4198{
4199 return HW_IsPrivilegedContext();
4200}
4201
4203stk::hw::HiResClock::HiResClock() : stk::hw::detail::HiResClockState()
4204{
4205#if (__CORTEX_M >= 3U)
4206 HW_DWTEnableCounter();
4207 m_prev = HW_DWTGetCounter();
4208#endif
4209}
4210
4213{
4214#if (__CORTEX_M >= 3U)
4215 // high resolution clock for Cortex-M3+ using DWT peripheral
4216 const uint32_t current = HW_DWTGetCounter();
4217
4218 // unsigned subtraction handles the wrap-around perfectly
4219 const uint32_t delta = current - m_prev;
4220 m_acc += delta;
4221
4222 m_prev = current;
4223
4224 return m_acc;
4225#else
4226 // high resolution clock for Cortex-M0, stateless
4227 const Ticks resolution_ticks = static_cast<Ticks>(GetContext().m_tick_resolution);
4228 const Cycles tick_cycles = ConvertTimeUsToClockCycles(HW_CoreClockFrequency(), resolution_ticks);
4229
4230 // tick counter cannot advance while the critical section is held (SysTick ISR is deferred)
4231 Cycles cycles = ConvertTimeUsToClockCycles(HW_CoreClockFrequency(), (stk::GetTicks() * resolution_ticks));
4232
4233 bool wrapped = ((SCB->ICSR & SCB_ICSR_PENDSTSET_Msk) != 0U);
4234 uint32_t val = HW_SysTickValue();
4235 if (!wrapped && ((SCB->ICSR & SCB_ICSR_PENDSTSET_Msk) != 0U))
4236 {
4237 // counter wrapped between the two reads: re-sample the post-wrap value
4238 wrapped = true;
4239 val = HW_SysTickValue();
4240 }
4241 if (wrapped)
4242 {
4243 cycles += tick_cycles; // the tick whose ISR is still pending
4244 }
4245
4246 const uint32_t load = SysTick->LOAD;
4247 const uint32_t delta = load - val;
4248
4249 const Cycles delta_cycles = static_cast<Cycles>(delta);
4250
4251 // total elapsed cycles
4252 return cycles + delta_cycles;
4253#endif
4254}
4255
4257uint32_t stk::hw::HiResClock::GetFrequency() const
4258{
4259 const uint32_t freq = HW_CoreClockFrequency();
4260 STK_ASSERT(freq != 0U);
4261 return freq;
4262}
4263
4264#if STK_TLS && !STK_INLINE_TLS
4265Word stk::hw::GetTls()
4266{
4267 return GetContext().GetTls();
4268}
4269
4270void stk::hw::SetTls(Word tp)
4271{
4272 GetContext().SetTls(tp);
4273}
4274#endif // STK_TLS && !STK_INLINE_TLS
4275
4276// ----------------------------------------------------------------------------
4277// MPU management API
4278// ----------------------------------------------------------------------------
4279
4280// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4281#if STK_MPU
4282// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4283
4284void hw_int::pmsa::ConfigureRegion(MpuRegion &reg, const struct MpuRegionConfig &cfg, uint32_t region_idx)
4285{
4286#if STK_ARCH_ARMV8_M
4287 if ((cfg.size != 0U) && (cfg.access_perm != hw::mpu::EMpuAccess::ACCESS_NONE))
4288 {
4289 // hardware rules: both Base and Limit addresses must be 32-byte aligned.
4290 STK_ASSERT((cfg.addr & 31U) == 0U);
4291 STK_ASSERT((cfg.size & 31U) == 0U);
4292
4293 // calculate RBAR: Base address | Shareability [4:3] | AP permissions [2:1] | Execution state [0]
4294 reg.addr = (cfg.addr & ~31U) |
4295 static_cast<uint32_t>(cfg.share) |
4296 static_cast<uint32_t>(cfg.access_perm) |
4297 static_cast<uint32_t>(cfg.exec);
4298
4299 // calculate RLAR: Final 32-byte chunk base address | MAIR allocation index [3:1] | Enable Flag [0]
4300 // find the absolute last byte address within the protected region
4301 const uint32_t end_byte_addr = cfg.addr + cfg.size - 1U;
4302 // clear the bottom 5 bits to isolate the base of the final 32-byte window
4303 const uint32_t limit_addr = end_byte_addr & ~31U;
4304 reg.attr = limit_addr | (static_cast<uint32_t>(cfg.mem_type) << 1U) | RLAR_ENABLE_FLAG;
4305 }
4306 else
4307 {
4308 // disabled region
4309 reg.addr = RBAR_DISABLED_REGION(region_idx);
4310 reg.attr = RLAR_DISABLED_REGION;
4311 }
4312#else
4313 // legacy ARMv7-M Attribute layout
4314 if ((cfg.size != 0U) && (cfg.access_perm != hw::mpu::EMpuAccess::ACCESS_NONE))
4315 {
4316 // hardware rules: must be a strict power-of-two size, naturally aligned
4317 STK_ASSERT((cfg.size & (cfg.size - 1U)) == 0U);
4318 STK_ASSERT((cfg.addr & (cfg.size - 1U)) == 0U);
4319
4320 // active ARMv7-M Base layout: masked completely by its structural size constraint
4321 reg.addr = (cfg.addr & ~(cfg.size - 1U)) | (1U << 4U) | (region_idx & 0xFU);
4322
4323 // active Region
4324 const uint32_t size_field = static_cast<uint32_t>(31U - CountLeadingZeros(cfg.size)) - 1U;
4325
4326 // defensive masking: Protect bits [26:24] against sentinel values or dirty enum extensions
4327 const uint32_t ap_bits = static_cast<uint32_t>(cfg.access_perm) & 0x07000000U;
4328
4329 const uint32_t attr = (ap_bits |
4330 static_cast<uint32_t>(cfg.share) |
4331 static_cast<uint32_t>(cfg.mem_type) |
4332 static_cast<uint32_t>(cfg.exec));
4333
4334 reg.attr = attr | (size_field << 1U) | RASR_ENABLE_FLAG;
4335 }
4336 else
4337 {
4338 // disabled region: clear out base address entirely to prevent false matches,
4339 // point to the target hardware index slot and strip the RASR enable flag
4340 reg.addr = RBAR_DISABLED_REGION(region_idx);
4341 reg.attr = RASR_DISABLED_REGION;
4342 }
4343#endif
4344}
4345
4346// ----------------------------------------------------------------------------
4347// Make file local content (anonymous namespace).
4348namespace {
4349// ----------------------------------------------------------------------------
4350
4352__stk_forceinline MPU_Type *GetMpu(bool non_secure)
4353{
4354#if STK_ARCH_ARMV8_M && STK_TZ_SECURE
4355 return non_secure ? MPU_NS : MPU;
4356#else
4357 STK_UNUSED(non_secure); // no Secure/Non-Secure MPU alias
4358 return MPU;
4359#endif
4360}
4361
4363__stk_forceinline SCB_Type *GetScb(bool non_secure)
4364{
4365#if STK_ARCH_ARMV8_M && STK_TZ_SECURE
4366 return non_secure ? SCB_NS : SCB;
4367#else
4368 STK_UNUSED(non_secure); // no Secure/Non-Secure SCB alias
4369 return SCB;
4370#endif
4371}
4372
4373// ----------------------------------------------------------------------------
4374} // namespace
4375// ----------------------------------------------------------------------------
4376
4377void hw_int::pmsa::ApplyRegion(const MpuRegion &reg, uint32_t index, bool non_secure)
4378{
4379 MPU_Type *const MPU_ptr = GetMpu(non_secure);
4380
4381 MPU_ptr->RNR = index;
4382 __DSB();
4383 __ISB();
4384
4385 MPU_ptr->RBAR = reg.addr;
4386#if STK_ARCH_ARMV8_M
4387 MPU_ptr->RLAR = reg.attr;
4388#else
4389 MPU_ptr->RASR = reg.attr;
4390#endif
4391}
4392
4393void hw_int::pmsa::Enable(bool enable, uint32_t control_flags, bool non_secure)
4394{
4395 __stk_dmb();
4396
4397 MPU_Type *const MPU_ptr = GetMpu(non_secure);
4398 SCB_Type *const SCB_ptr = GetScb(non_secure);
4399
4400 if (enable)
4401 {
4402 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
4403 SCB_ptr->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
4404 #endif
4405 MPU_ptr->CTRL = control_flags | MPU_CTRL_ENABLE_Msk;
4406 }
4407 else
4408 {
4409 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
4410 SCB_ptr->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
4411 #endif
4412 MPU_ptr->CTRL &= ~MPU_CTRL_ENABLE_Msk;
4413 }
4414
4415 __DSB();
4416 __ISB();
4417}
4418
4419void hw_int::pmsa::DisableRegion(uint32_t index, bool non_secure)
4420{
4421 MPU_Type *const MPU_ptr = GetMpu(non_secure);
4422
4423 MPU_ptr->RNR = index;
4424
4425#if STK_ARCH_ARMV8_M
4426 MPU_ptr->RLAR = RLAR_DISABLED_REGION;
4427#else
4428 MPU_ptr->RASR = RASR_DISABLED_REGION;
4429#endif
4430}
4431
4432void hw_int::pmsa::ConfigureStatic(const MpuRegionConfig cfg_list[], size_t cfg_count, uint32_t control_flags,
4433 bool non_secure)
4434{
4435 STK_ASSERT((cfg_list != nullptr) || (cfg_count == 0U));
4436
4437 const size_t region_idx = (non_secure ? STK_CORTEX_M_MPU_TASK_REGION_IDX_NS : STK_CORTEX_M_MPU_TASK_REGION_IDX);
4438 STK_ASSERT((cfg_count <= region_idx) || (region_idx == 0U));
4439
4440 // validate static table configuration against internal overlaps before writing hardware
4441 ValidateNoOverlaps(nullptr, 0U, cfg_list, cfg_count);
4442
4443 Enable(false, 0U, non_secure);
4444
4445 // cfg_list[i] always targets hardware region i: static regions occupy a fixed,
4446 // contiguous block starting at region 0, so the index is derived from array
4447 // position rather than being supplied by the caller
4448 size_t index = 0U;
4449 for (; index < cfg_count; ++index)
4450 {
4451 const MpuRegionConfig &cfg = cfg_list[index];
4452
4453 MpuRegion reg;
4454 ConfigureRegion(reg, cfg, static_cast<uint32_t>(index));
4455 ApplyRegion(reg, static_cast<uint32_t>(index), non_secure);
4456 }
4457
4458 // disable every remaining static-region slot so a shorter table than a previous
4459 // call can never leave stale configuration active in hardware; callers no longer
4460 // need to supply placeholder/empty entries just to occupy unused regions
4461 for (; index < region_idx; ++index)
4462 {
4463 MpuRegion reg;
4464 ConfigureRegion(reg, s_StkDisabledMpuRegion, static_cast<uint32_t>(index));
4465 ApplyRegion(reg, static_cast<uint32_t>(index), non_secure);
4466 }
4467
4468 // configure memory attributes and point to the start of the per-task region beginning
4469 // note: ARMv7-M does not require RNR to be set as region index is embedded into RBAR,
4470 // though we do it for a consistency with ARMv8-M here
4471 {
4472 MPU_Type *const MPU_ptr = GetMpu(non_secure);
4473
4474 #if STK_ARCH_ARMV8_M
4475 // configure memory attributes
4476 MPU_ptr->MAIR0 = MAIR0_PMSAV8_INIT;
4477 MPU_ptr->MAIR1 = MAIR1_PMSAV8_INIT;
4478 #endif
4479
4480 // point to the start of the per-task config area
4481 MPU_ptr->RNR = region_idx;
4482 }
4483
4484 Enable(true, control_flags, non_secure);
4485}
4486
4487void hw_int::pmsa::ConfigureDynamic(TaskMpu &task_mpu, const struct MpuRegionConfig cfg_list[],
4488 const size_t cfg_count, IPlatform::IEventOverrider *overrider, bool non_secure)
4489{
4490 STK_ASSERT((cfg_list != nullptr) || (cfg_count == 0U));
4491
4492 const size_t region_idx = (non_secure ? STK_CORTEX_M_MPU_TASK_REGION_IDX_NS : STK_CORTEX_M_MPU_TASK_REGION_IDX);
4493 const size_t task_mpu_regions = (non_secure ? TaskMpuNs::NUM_REGIONS : TaskMpu::NUM_REGIONS);
4494 STK_ASSERT(cfg_count <= task_mpu_regions);
4495
4496 const size_t clamped_count = Min(cfg_count, task_mpu_regions);
4497
4498 // retrieve static regions directly via overrider getter
4499 MpuRegionList static_regions(nullptr, 0);
4500 if (overrider != nullptr)
4501 {
4502 const MpuConfig *const cfg = overrider->OnConfigureMpu();
4503 if (cfg != nullptr)
4504 {
4505 static_regions = cfg->regions;
4506 }
4507 }
4508
4509 // validate task region list internal consistency
4510 ValidateNoOverlaps(static_regions.GetPtr(), static_regions.GetSize(), cfg_list, clamped_count);
4511
4512 // cfg_list[i] always targets task-relative slot i (hardware region region_idx + i): the index
4513 // is derived from array position rather than being supplied by the caller
4514 size_t task_idx = 0U;
4515 for (; task_idx < clamped_count; ++task_idx)
4516 {
4517 ConfigureRegion(task_mpu.region[task_idx], cfg_list[task_idx],
4518 static_cast<uint32_t>(region_idx + task_idx));
4519 }
4520
4521 // fill any remaining task slots with a disabled-region descriptor so callers no
4522 // longer need to supply placeholder/empty entries just to occupy unused slots;
4523 // this only updates the in-memory shadow table, it takes effect in hardware on
4524 // the next context switch into this task, via STK_ASM_BLOCK_MPU_STACK_GUARD
4525 for (; task_idx < task_mpu_regions; ++task_idx)
4526 {
4527 ConfigureRegion(task_mpu.region[task_idx], s_StkDisabledMpuRegion,
4528 static_cast<uint32_t>(region_idx + task_idx));
4529 }
4530}
4531
4532// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4533#endif // STK_MPU
4534// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4535
4536// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4537#endif // !defined(_STK_CORTEX_M_TRUSTZONE_NON_SECURE)
4538// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4539
4540#if STK_MPU
4541// ----------------------------------------------------------------------------
4542namespace stk {
4543// ----------------------------------------------------------------------------
4544void FaultContext_FillMpu(FaultContext::Mpu &mpu_ctx, MPU_Type *mpu_inst)
4545{
4546 mpu_ctx.CTRL = mpu_inst->CTRL;
4547#if STK_ARCH_ARMV8_M
4548 mpu_ctx.MAIR0 = mpu_inst->MAIR0;
4549 mpu_ctx.MAIR1 = mpu_inst->MAIR1;
4550#endif
4551
4552 const Word saved_rnr = mpu_inst->RNR;
4553 for (size_t i = 0U; i < STK_STATIC_ARRAY_SIZE(mpu_ctx.regions); ++i)
4554 {
4555 mpu_inst->RNR = static_cast<Word>(i);
4556 __DSB();
4557 __ISB();
4558 mpu_ctx.regions[i].RNR = static_cast<Word>(i);
4559 mpu_ctx.regions[i].RBAR = mpu_inst->RBAR;
4560 #if STK_ARCH_ARMV8_M
4561 mpu_ctx.regions[i].ATTR = mpu_inst->RLAR;
4562 #else
4563 mpu_ctx.regions[i].ATTR = mpu_inst->RASR;
4564 #endif
4565 }
4566 mpu_inst->RNR = saved_rnr;
4567}
4568// ----------------------------------------------------------------------------
4569} // namespace stk
4570// ----------------------------------------------------------------------------
4571#endif // STK_MPU
4572
4573void FaultContext::Fill(const Word *stacked_regs, Word exc_return)
4574{
4575 this->frame.R0 = stacked_regs[0];
4576 this->frame.R1 = stacked_regs[1];
4577 this->frame.R2 = stacked_regs[2];
4578 this->frame.R3 = stacked_regs[3];
4579 this->frame.R12 = stacked_regs[4];
4580 this->frame.LR = stacked_regs[5];
4581 this->frame.PC = stacked_regs[6];
4582 this->frame.xPSR = stacked_regs[7];
4583
4584 this->EXC_RETURN = exc_return;
4585 this->CONTROL = __get_CONTROL();
4586
4587#if STK_ARCH_ARMV6_M
4588 // ARMv6-M (Cortex-M0/M0+) does not have CFSR, HFSR, AFSR, MMFAR, or BFAR.
4589 this->CFSR = 0U;
4590 this->HFSR = 0U;
4591 this->AFSR = 0U;
4592 this->mmfar_valid = false;
4593 this->bfar_valid = false;
4594 this->MMFAR = 0U;
4595 this->BFAR = 0U;
4596#else
4597 // ARMv7-M / ARMv8-M
4598 this->CFSR = SCB->CFSR;
4599 this->HFSR = SCB->HFSR;
4600 this->AFSR = SCB->AFSR;
4601 this->mmfar_valid = ((this->CFSR & SCB_CFSR_MMARVALID_Msk) != 0U);
4602 this->bfar_valid = ((this->CFSR & SCB_CFSR_BFARVALID_Msk) != 0U);
4603 this->MMFAR = (this->mmfar_valid ? SCB->MMFAR : 0U);
4604 this->BFAR = (this->bfar_valid ? SCB->BFAR : 0U);
4605#endif // STK_ARCH_ARMV6_M
4606
4607#if STK_MPU
4608 FaultContext_FillMpu(this->mpu, MPU);
4609 #if STK_ARCH_ARMV8_M && STK_TZ_SECURE
4610 FaultContext_FillMpu(this->mpu_ns, MPU_NS);
4611 #endif
4612#endif // STK_MPU
4613}
4614
4615#endif // _STK_ARCH_ARM_CORTEX_M
#define STK_ASM_POOL
#define STK_ASM_SYNTAX_UNIFIED
#define STK_ASM_ALIGN_2
#define STK_ASM_EXTRACT_STACK_POINTER_TO_R0
#define STK_ASM_GLOBAL_SYMBOL(name)
Generates a toolchain-agnostic assembly directive to declare a global symbol.
static void __stk_dmb()
Hardware memory barrier: ensures visibility across cores and bus masters.
#define STK_TZ_NSC_GATEWAY
ARM TrustZone: declares a Non-Secure callable gateway function (extern "C", cmse_nonsecure_entry).
#define STK_CORTEX_M_MPU_REGIONS_MAX
Number of MPU regions supported by MPU peripheral.
#define STK_CORTEX_M_SVCALL_ISR_PRIORITY
Priority of the SVCall exception (the most urgent one used by the kernel).
Contains common inventory for platform implementation.
#define STK_ARCH_GET_CPU_ID()
Get CPU core id of the caller, e.g. if called while running on core 0 then returned value must be 0.
#define GetContext()
Get platform's context.
Top-level STK include. Provides the Kernel class template and all built-in task-switching strategies.
Hardware Abstraction Layer (HAL) declarations for the stk::hw namespace.
void STK_PANIC_HANDLER_DEFAULT(stk::EKernelPanicId id)
Default panic handler: disable interrupts, record the id, and spin in a tight loop - a defined,...
#define STK_MPU_SHARED_CODE_SECTION
Definition stk_arch.h:159
#define STK_MPU_KERNEL_DATA_SECTION
Definition stk_arch.h:161
#define STK_MPU_KERNEL_BSS_SECTION
Definition stk_arch.h:163
#define STK_MPU_KERNEL_CODE_SECTION
Definition stk_arch.h:162
#define STK_MPU_SHARED_DATA_SECTION
Definition stk_arch.h:158
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
Definition stk_defs.h:715
#define __stk_attr_used
Marks a symbol as used, preventing the linker from discarding it even if no references are visible (d...
Definition stk_defs.h:339
#define __stk_forceinline
Forces compiler to always inline the decorated function, regardless of optimisation level.
Definition stk_defs.h:270
#define STK_SLEEP_TRAP_STACK_SIZE
Stack size for the sleep trap in elements of Word (default: STK_STACK_SIZE_MIN).
Definition stk_defs.h:659
#define STK_CS_NESTINGS_MAX
Maximum allowable recursion depth for critical section entry (default: 16).
Definition stk_defs.h:597
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:532
#define STK_STACK_SIZE_MIN
Minimum stack size in elements of Word, shared by all stack allocation lower-bound checks.
Definition stk_defs.h:646
#define STK_MPU_TASK_REGIONS
Number of hardware MPU region slots reserved per task (stk::TaskMpu::NUM_REGIONS).
Definition stk_defs.h:155
#define __stk_attr_noinline
Prevents compiler from inlining the decorated function (function prefix).
Definition stk_defs.h:350
#define STK_STATIC_ASSERT_DESC_N(NAME, X, DESC)
Compile-time assertion with a name tag and a custom error description.
Definition stk_defs.h:543
#define STK_ARCH_CPU_COUNT
Number of physical CPU cores available to the scheduler (default: 1).
Definition stk_defs.h:606
#define __stk_attr_naked
Suppresses compiler-generated function prologue and epilogue (function prefix).
Definition stk_defs.h:306
#define STK_MPU_TASK_REGIONS_NS
Definition stk_defs.h:158
#define STK_STACK_MEMORY_FILLER
Sentinel value written to the entire stack region at initialization (stack watermark pattern).
Definition stk_defs.h:570
#define __stk_attr_unused
Suppresses compiler warnings about an unused type, variable, or function (declaration prefix).
Definition stk_defs.h:328
#define __stk_attr_noreturn
Declares that function never returns to its caller (function prefix).
Definition stk_defs.h:317
static void __stk_debug_break()
Definition stk_defs.h:495
#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
#define STK_VIRT_DTOR
Expands to nothing if STK_STRICT_COMPLIANCY=0 (non-virtual destructors, smaller objects),...
Definition stk_defs.h:254
#define STK_PACKAGE_NAME
Name of package.
Namespace of STK package.
uintptr_t Word
Native processor word type.
Definition stk_common.h:206
static constexpr TId TID_ISR_N
Bitmask sentinel for ISR-context task identifiers.
Definition stk_common.h:263
void STK_KERNEL_PANIC(stk::EKernelPanicId id)
Called when the kernel detects an unrecoverable internal fault.
Definition stk_arch.h:184
@ ACCESS_PRIVILEGED
Privileged access mode (access to hardware is fully unrestricted).
Definition stk_common.h:38
@ ACCESS_SECURE
Secure access mode (ARM TrustZone, Secure binary).
Definition stk_common.h:39
static constexpr Timeout NO_WAIT
Timeout value: return immediately if the synchronization object is not yet signaled (non-blocking pol...
Definition stk_common.h:280
EWaitResult
Wait result (see IKernelService::Wait).
Definition stk_common.h:138
static __stk_forceinline Cycles ConvertTimeUsToClockCycles(uint32_t clock_freq, Ticks time_us)
Convert time (microseconds) to core clock cycles.
int64_t Ticks
Ticks value.
Definition stk_common.h:221
EKernelPanicId
Identifies the source of a kernel panic.
Definition stk_common.h:60
@ KERNEL_PANIC_UNKNOWN_SVC
Unknown service command received by SVC handler.
Definition stk_common.h:68
@ KERNEL_PANIC_BAD_STACK_TYPE
Stack type is unknown.
Definition stk_common.h:71
@ KERNEL_PANIC_NS_ACCESS
Non-secure access to protected resource.
Definition stk_common.h:72
@ KERNEL_PANIC_HRT_HARD_FAULT
Kernel running in KERNEL_HRT mode reported deadline failure of the task.
Definition stk_common.h:65
@ KERNEL_PANIC_NONE
Panic is absent (no fault).
Definition stk_common.h:61
@ KERNEL_PANIC_SPINLOCK_DEADLOCK
Spin-lock timeout expired: lock owner never released.
Definition stk_common.h:62
int32_t Timeout
Timeout time (ticks).
Definition stk_common.h:216
constexpr T Max(T a, T b) noexcept
Compile-time maximum of two values.
Definition stk_defs.h:758
constexpr T Min(T a, T b) noexcept
Compile-time minimum of two values.
Definition stk_defs.h:752
uint32_t CountLeadingZeros(const uint32_t value) noexcept
Count leading zeros.
Definition stk_defs.h:791
ELockResult
Result of IKernel::SetSchedulerLock(), IKernelService::LockScheduler(), UnlockScheduler() and Restore...
Definition stk_common.h:161
@ SYS_TASK_ID_SLEEP
Sleep trap.
Definition stk_common.h:117
static constexpr bool IsIsrTid(TId id)
Test whether a task identifier represents an ISR context.
Definition stk_common.h:309
static Ticks GetTicks()
Get number of ticks elapsed since kernel start.
Definition stk_helper.h:448
static constexpr TId TID_NONE
Reserved task/thread id representing zero/none thread id.
Definition stk_common.h:268
@ TRACE_EVENT_SWITCH
Task blocked by the context switch.
Definition stk_common.h:128
EStackType
Stack type.
Definition stk_common.h:96
@ STACK_SLEEP_TRAP
Stack of the Sleep trap.
Definition stk_common.h:98
@ STACK_USER_TASK
Stack of the user task.
Definition stk_common.h:97
@ STACK_EXIT_TRAP
Stack of the Exit trap.
Definition stk_common.h:99
uint64_t Cycles
Cycles value.
Definition stk_common.h:231
EHwException
Hardware exception id (see IPlatform::IEventOverrider::OnException).
Definition stk_common.h:194
@ HW_EXCEPT_MEMACCESS
MemManage on ARM / Page Fault or PMP violation on RISC-V.
Definition stk_common.h:196
@ HW_EXCEPT_FATAL
HardFault on ARM / Unhandled Fatal Trap on RISC-V.
Definition stk_common.h:195
Word TId
Task (thread) id.
Definition stk_common.h:211
int32_t Weight
Weight value (aka priority).
Definition stk_common.h:236
bool IsPrivilegedContext()
Check if caller context is Privileged.
static T * WordToPtr(Word value) noexcept
Cast a CPU register-width integer back to a pointer.
Definition stk_arch.h:232
static Word PtrToWord(T *const ptr) noexcept
Cast a pointer to a CPU register-width integer.
Definition stk_arch.h:215
bool IsInsideISR()
Check whether the CPU is currently executing inside a hardware interrupt service routine (ISR).
void ProcessTick() override
Process one tick.
Word GetCallerSP() const override
Get caller's Stack Pointer (SP).
void ProcessHardFault() override
Cause a hard fault of the system.
void ForceContextSwitch(TId id) override
Force context switch.
uint32_t GetTickResolution() const override
Get resolution of the system tick timer in microseconds. Resolution means a number of microseconds be...
Timeout Suspend() override
Suspend scheduling.
void Start() override
Start scheduling.
void SetCpuFrequency(uint8_t core_id, uint32_t frequency) override
Notify the scheduler of a CPU core's operating frequency.
void Stop() override
Stop scheduling.
TId GetTid() const override
Get thread Id.
void SetEventOverrider(IEventOverrider *overrider, bool non_secure) override
Set platform event overrider.
uint32_t GetSysTimerFrequency() const override
Get system timer frequency.
Cycles GetSysTimerCount() const override
Get system timer count value.
void InitStack(EStackType stack_type, Stack *stack, IStackMemory *stack_memory, ITask *user_task) override
Initialize stack memory of the user task.
void Resume(Timeout elapsed_ticks) override
Resume scheduling after a prior Suspend() call.
void Initialize(IEventHandler *event_handler, IKernelService *service, uint32_t resolution_us, Stack *exit_trap) override
Initialize scheduler's context.
ARMv7-M/ARMv8-M hardware exception frame (8 words, highest address on the stack).
ARMv7-M/ARMv8-M system fault exception context state capture.
void Fill(const Word *stacked_regs, Word exc_return)
Populate fault context registers and hardware MPU state from an exception stack frame.
Hardware Memory Protection Unit (MPU) status register snapshot.
Region regions[(8U)]
Active hardware MPU region register state table.
Word CTRL
MPU Control Register (MPU_CTRL).
Word RNR
Region Number Register (RNR).
Word ATTR
Region Attribute and Size Register (RASR / RLAR / MPU_RLAR).
Word RBAR
Region Base Address Register (RBAR).
Base platform context for all platform implementations.
static Session Enter(const Session ses=DEFAULT_SESSION)
Enter a critical section.
uint8_t Session
Opaque session token returned by Enter() and consumed by Exit().
Definition stk_arch.h:472
static void Exit(const Session ses=DEFAULT_SESSION)
Exit a critical section.
@ SESSION_FLAG_NPRIV
Calling context is non-Privileged.
Definition stk_arch.h:465
RAII instance that enters the critical section on construction and exits it on destruction.
Definition stk_arch.h:495
Atomic busy-wait lock used as the global cross-core synchronisation primitive inside CriticalSection.
Definition stk_arch.h:571
bool TryLock()
Attempt to acquire SpinLock in a single non-blocking attempt.
void Lock()
Acquire SpinLock, blocking until it is available.
void Unlock()
Release SpinLock, allowing another thread or core to acquire it.
High-resolution clock for high-precision measurements.
Definition stk_arch.h:722
HiResClock()
Construct a clock instance.
uint32_t GetFrequency() const
Get clock frequency.
Cycles GetCycles()
Get number of clock cycles elapsed since the time origin (see the class description).
Stack descriptor.
Definition stk_common.h:473
uint32_t access_mode
Bitfield with hardware access mode of the task (see EAccessMode).
Definition stk_common.h:475
Word SP
Offset 0: Stack Pointer (SP) register (note: must always be at offset 0).
Definition stk_common.h:474
Interface for a stack memory region.
Definition stk_common.h:494
virtual size_t GetStackSize() const =0
Get number of elements of the stack memory array.
virtual const Word * GetStack() const =0
Get pointer to the stack memory.
Synchronization object interface.
Definition stk_common.h:659
Interface for mutex synchronization primitive.
Definition stk_common.h:817
Interface for a user task.
Definition stk_common.h:886
virtual void Run()=0
Entry point of the user task.
Interface for a platform event overrider.
Interface for the kernel services exposed to the user processes during run-time when Kernel started s...
virtual TId GetTid() const =0
Get thread Id of the currently running task.
virtual Timeout SetTimeSlice(TId tid, Timeout ticks)=0
Change the time-slice quantum of the task at run-time and reload its slice.
virtual void Wake(ISyncObject *sobj, bool all)=0
Wake one or all tasks currently waiting on a synchronization object.
virtual ELockResult UnlockScheduler()=0
Enable task switching (equivalent of CMSIS-RTOS2 osKernelUnlock).
virtual void SleepCancel(TId task_id)=0
Cancel sleep of the task.
virtual void InheritWeight(TId tid, Weight weight)=0
Inherit weight for the task.
virtual bool Sleep(Timeout ticks)=0
Put calling process into a sleep state.
static IKernelService * GetInstance()
Get CPU-local instance of the kernel service.
virtual uint32_t GetTickResolution() const =0
Get number of microseconds in one tick.
virtual bool SleepUntil(Ticks timestamp)=0
Put calling process into a sleep state until the specified timestamp.
virtual Ticks GetTicks() const =0
Get number of ticks elapsed since kernel start.
virtual void SwitchToNext()=0
Notify scheduler to switch to the next task (yield).
virtual void Resume(Timeout elapsed_ticks)=0
Resume scheduling after a prior Suspend() call.
virtual Weight SetWeight(TId tid, Weight weight)=0
Change the base weight of the task at run-time (replaces ITask::GetWeight() value).
virtual void ExitTask()=0
Terminate the calling task: schedule its removal and switch it out. Does not return.
virtual EWaitResult Wait(ISyncObject *sobj, IMutex *mutex, Timeout timeout)=0
Put calling process into a waiting state until synchronization object is signaled or timeout occurs.
virtual ELockResult LockScheduler()=0
Disable task switching (equivalent of CMSIS-RTOS2 osKernelLock). Ticks, timeouts and ISRs keep runnin...
virtual ELockResult RestoreSchedulerLock(bool locked)=0
Set the lock state (equivalent of CMSIS-RTOS2 osKernelRestoreLock).
virtual Cycles GetSysTimerCount() const =0
Get system timer count value.
virtual uint32_t GetSysTimerFrequency() const =0
Get system timer frequency.
virtual Timeout Suspend()=0
Suspend scheduling.
virtual void RestoreWeight(TId tid, ISyncObject *sobj=nullptr)=0
Restore weight of the task to the original value.
virtual void Delay(Timeout ticks)=0
Delay calling process.