This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 96f824e96e5 arch/arm64: add TrustZone idle world-switch and secure SGI 
support
96f824e96e5 is described below

commit 96f824e96e5d207fc96574281ff818491d13ff0e
Author: hujun5 <[email protected]>
AuthorDate: Tue Apr 21 11:33:50 2026 +0800

    arch/arm64: add TrustZone idle world-switch and secure SGI support
    
    Add TrustZone dual-OS support for arm64:
    
    - arm64_idle.c: implement world-switch in idle loop for TEE/REE
      coexistence. When TEE has no tasks, issue NUTTXSPD_TEE_IDLE SMC
      to yield CPU to REE; when REE is idle, issue NUTTXSPD_REE_IDLE
      to yield back to TEE.
    
    - arm64_gicv3.c: add arm64_send_secure_sgi() for non-secure world
      to request EL3 to send a Group 1 Secure SGI via
      NUTTXSPD_SEND_SECURE_SGI SMC, enabling cross-world interrupt
      signaling.
    
    Signed-off-by: hujun5 <[email protected]>
---
 arch/arm64/src/common/arm64_gicv3.c | 19 +++++++++++++-
 arch/arm64/src/common/arm64_idle.c  | 51 +++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/src/common/arm64_gicv3.c 
b/arch/arm64/src/common/arm64_gicv3.c
index afab8302504..86b7e6bcc56 100644
--- a/arch/arm64/src/common/arm64_gicv3.c
+++ b/arch/arm64/src/common/arm64_gicv3.c
@@ -844,7 +844,24 @@ void up_trigger_irq(int irq, cpu_set_t cpuset)
 
   if (GIC_IS_SGI(irq))
     {
-      arm64_gic_raise_sgi(irq, cpuset);
+#ifdef CONFIG_ARCH_TRUSTZONE_NONSECURE
+      /* If this SGI is configured as secure (bit=0 in
+       * IGROUPR_SGI_SECURE_VAL), REE cannot send it directly.
+       * Use SMC to request the secure world to trigger it.
+       */
+
+      if (!(IGROUPR_SGI_SECURE_VAL & BIT(irq)))
+        {
+          struct arm64_smccc_res res;
+
+          arm64_smccc_smc(ARM64_SMCCC_NUTTX_SEND_SECURE_SGI,
+                          irq, 0, 0, 0, 0, 0, 0, &res);
+        }
+      else
+#endif
+        {
+          arm64_gic_raise_sgi(irq, cpuset);
+        }
     }
   else if (irq >= 0 && irq < NR_IRQS)
     {
diff --git a/arch/arm64/src/common/arm64_idle.c 
b/arch/arm64/src/common/arm64_idle.c
index 1424e213b7d..fd8db6fc86d 100644
--- a/arch/arm64/src/common/arm64_idle.c
+++ b/arch/arm64/src/common/arm64_idle.c
@@ -55,6 +55,57 @@ void up_idle(void)
    */
 
   nxsched_process_timer();
+#elif defined(CONFIG_ARCH_TRUSTZONE_SECURE)
+
+  /* Secure OS idle.
+   *
+   * Every entry calls ARM64_SMCCC_NUTTX_TEE_IDLE:
+   *   - First call: EL3 registers interrupt handlers and boots REE.
+   *   - Subsequent calls: EL3 checks if REE was preempted by G1S.
+   *     If so, resumes REE. Otherwise returns 0 and TEE does WFI.
+   *
+   * TEE runs again when:
+   *   1. Secure timer fires from REE -> EL3 does world switch ->
+   *      TEE resumes here (after SMC/WFI) -> pending G1S IRQ
+   *      fires through normal S-EL1 vector table -> threads run ->
+   *      all done -> back here -> ARM64_SMCCC_NUTTX_TEE_IDLE -> resume REE
+   *   2. TEE was preempted by NS interrupt -> REE idle sends
+   *      ARM64_SMCCC_NUTTX_REE_IDLE -> EL3 switches to TEE -> TEE
+   *      continues -> eventually idle -> ARM64_SMCCC_NUTTX_TEE_IDLE here
+   */
+
+  struct arm64_smccc_res res;
+
+  /* TEE idle: on first call, EL3 registers interrupt handlers
+   * and switches to REE. On subsequent calls, checks if REE
+   * was preempted by G1S and resumes it, or returns for WFI.
+   *
+   * Disable IRQs to prevent a lost-wakeup race: if a G1S IRQ
+   * were taken between SMC return and WFI, its handler could
+   * wake tasks but WFI would still sleep. With IRQs masked,
+   * the pending interrupt keeps WFI from blocking.
+   */
+
+  irqstate_t flags = irq_save();
+
+  arm64_smccc_smc(ARM64_SMCCC_NUTTX_TEE_IDLE, 0, 0, 0, 0, 0, 0, 0, &res);
+
+  asm("dsb sy");
+  asm("wfi");
+
+  irq_restore(flags);
+
+#elif defined(CONFIG_ARCH_TRUSTZONE_NONSECURE)
+
+  /* REE idle: no tasks ready, yield CPU to TEE unconditionally.
+   * TEE will WFI. When a NS interrupt fires, EL3 preempts TEE
+   * and returns here. When a G1S fires, EL3 switches to TEE
+   * and TEE_IDLE will resume us later.
+   */
+
+  struct arm64_smccc_res res;
+
+  arm64_smccc_smc(ARM64_SMCCC_NUTTX_REE_IDLE, 0, 0, 0, 0, 0, 0, 0, &res);
 #else
 
   /* Sleep until an interrupt occurs to save power */

Reply via email to