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

commit 15ac932651efadc868a35408ac234d978dea4702
Author: zhangyu117 <[email protected]>
AuthorDate: Mon Nov 24 14:09:49 2025 +0800

    arch/tricore: PPRS is part of task context and needs saving
    
    The processing flow for MPU PRS differs between TC3xx and TC4xx.
    TC3xx stores PRS directly in PSW, while TC4xx stores PRS in PPRS.
    Save and restore PPRS across context switches, signals, and nested
    syscalls so each task retains its protection register set.
    
    Signed-off-by: zhangyu117 <[email protected]>
---
 arch/tricore/Kconfig                               |  7 ++++
 arch/tricore/include/irq.h                         | 44 ++++++++++++++++++----
 arch/tricore/src/common/tricore_doirq.c            | 12 +++++-
 .../tricore/src/common/tricore_schedulesigaction.c |  2 +
 arch/tricore/src/common/tricore_sigdeliver.c       |  5 +++
 arch/tricore/src/common/tricore_svcall.c           | 28 ++++++++++++++
 6 files changed, 89 insertions(+), 9 deletions(-)

diff --git a/arch/tricore/Kconfig b/arch/tricore/Kconfig
index 4a246673d60..35edb65baaf 100644
--- a/arch/tricore/Kconfig
+++ b/arch/tricore/Kconfig
@@ -70,6 +70,7 @@ config ARCH_TC1V8
        select ARCH_HAVE_PERF_EVENTS
        select ARCH_HAVE_PERF_EVENTS_USER_ACCESS
        select ARCH_HAVE_POWEROFF
+       select ARCH_HAVE_PPRS
        select ARCH_HAVE_SETJMP
        select ARCH_HAVE_RESET
        select ARCH_SETJMP_H
@@ -147,6 +148,12 @@ config ARCH_MPU_CODE_NREGIONS
        ---help---
                The code regions count
 
+config ARCH_HAVE_PPRS
+       bool
+       --help--
+               The saving process for MPU PRS differs between TC1V6 and 
TC1V8.TC1V6
+               saves PRS in the PSW, while TC1V8 saves PRS in the PPRS 
register.
+
 config HAVE_SECURITY_CORE
        bool
        ---help---
diff --git a/arch/tricore/include/irq.h b/arch/tricore/include/irq.h
index 2d6b9d3d845..0897326bb35 100644
--- a/arch/tricore/include/irq.h
+++ b/arch/tricore/include/irq.h
@@ -46,6 +46,10 @@
 #include <arch/chip/irq.h>
 #include <arch/arch.h>
 
+#ifdef CONFIG_ARCH_HAVE_PPRS
+#  include <IfxCpu.h>
+#endif
+
 /****************************************************************************
  * Pre-processor Prototypes
  ****************************************************************************/
@@ -123,14 +127,32 @@
 
 #define PSW_PRS_KERNEL_SET  (0)
 #define PSW_PRS_USER_SET    (1)
-
-#ifdef CONFIG_ARCH_USE_MPU
-#  define PSW_PRS_MASK      ((3 << 12) | (1 << 15))
-#  define PSW_PRS_USER      (((PSW_PRS_USER_SET & 3) << 12) | \
-                             ((PSW_PRS_USER_SET & 4) << 13))
+#ifdef CONFIG_ARCH_HAVE_PPRS
+  #define PSW_PRS_MASK                           (0)
+  #define PSW_PRS_USER                           (0)
+  #define tricore_change_pprs(tcb, val)          (tcb)->xcp.pprs = (val)
+  #define tricore_load_pprs(tcb)                 (tcb)->xcp.pprs
+  #define tricore_restore_pprs(tcb)              __mtcr(CPU_PPRS, 
(tcb)->xcp.pprs)
+  #define tricore_store_pprs(tcb)                (tcb)->xcp.pprs = 
__mfcr(CPU_PPRS)
+  #define tricore_syscall_load_pprs(tcb ,index)  (tcb)->xcp.syscall_pprs[index]
+  #define tricore_syscall_store_pprs(tcb ,index) 
(tcb)->xcp.syscall_pprs[index] = __mfcr(CPU_PPRS)
+  #define tricore_sig_change_pprs(tcb ,val)      (tcb)->xcp.saved_pprs = (val)
+  #define tricore_sig_load_pprs(tcb)             (tcb)->xcp.saved_pprs
+  #define tricore_pprs_to_prs(pprs)              (((pprs & 3) << 12) | ((pprs 
& 4) << 13))
+  #define tricore_prs_to_pprs(prs)               (((prs >> 12) & 3) | ((prs >> 
13) & 4))
 #else
-#  define PSW_PRS_MASK      (0)
-#  define PSW_PRS_USER      (0)
+  #define PSW_PRS_MASK                           (3 << 12) | (1 << 15)
+  #define PSW_PRS_USER                           (((PSW_PRS_USER_SET & 3) << 
12) | ((PSW_PRS_USER_SET & 4) << 13))
+  #define tricore_change_pprs(tcb, val)          do { } while (0)
+  #define tricore_load_pprs(tcb)                 (0)
+  #define tricore_restore_pprs(tcb)              do { } while (0)
+  #define tricore_store_pprs(tcb)                do { } while (0)
+  #define tricore_syscall_load_pprs(tcb ,index)  (0)
+  #define tricore_syscall_store_pprs(tcb ,index) do { } while (0)
+  #define tricore_sig_change_pprs(tcb ,val)      do { } while (0)
+  #define tricore_sig_load_pprs(tcb)             (0)
+  #define tricore_pprs_to_prs(pprs)              (0)
+  #define tricore_prs_to_pprs(prs)               (0)
 #endif
 
 /* PCXI: Previous Context Information and Pointer Register */
@@ -209,6 +231,9 @@ struct xcptcontext
 #ifdef CONFIG_LIB_SYSCALL
   uint8_t nsyscalls;
   uintptr_t *syscall_regs[CONFIG_SYS_NNEST];
+#ifdef CONFIG_ARCH_HAVE_PPRS
+  uint32_t syscall_pprs[CONFIG_SYS_NNEST];
+#endif
 #endif
 
   /* Register save area with XCPTCONTEXT_SIZE, only valid when:
@@ -218,6 +243,11 @@ struct xcptcontext
    */
 
   uintptr_t *regs;
+
+#ifdef CONFIG_ARCH_HAVE_PPRS
+  uint32_t pprs;
+  uint32_t saved_pprs;
+#endif
 };
 
 /****************************************************************************
diff --git a/arch/tricore/src/common/tricore_doirq.c 
b/arch/tricore/src/common/tricore_doirq.c
index b502994f73a..271c844fbf1 100644
--- a/arch/tricore/src/common/tricore_doirq.c
+++ b/arch/tricore/src/common/tricore_doirq.c
@@ -98,6 +98,11 @@ void tricore_doirq(void)
       tcb = this_task();
 #endif
 
+      /* Updata PPRS register */
+
+      tricore_store_pprs(*running_task);
+      tricore_restore_pprs(tcb);
+
       /* Record the new "running" task when context switch occurred.
        * g_running_tasks[] is only used by assertion logic for reporting
        * crashes.
@@ -109,11 +114,14 @@ void tricore_doirq(void)
       UP_ISB();
     }
 
-  /* running_task->xcp.regs is about to become invalid
-   * and will be marked as NULL to avoid misusage.
+  /* (*running_task)->xcp.regs is about to become invalid and
+   * will be marked as NULL to avoid misusage. the same applies
+   * to (*running_task)->xcp.pprs.
    */
 
   (*running_task)->xcp.regs = NULL;
+  tricore_change_pprs(*running_task, UINT32_MAX);
+
   board_autoled_off(LED_INIRQ);
 #endif
 
diff --git a/arch/tricore/src/common/tricore_schedulesigaction.c 
b/arch/tricore/src/common/tricore_schedulesigaction.c
index ddefb43bb7b..27427c2f3ce 100644
--- a/arch/tricore/src/common/tricore_schedulesigaction.c
+++ b/arch/tricore/src/common/tricore_schedulesigaction.c
@@ -86,6 +86,7 @@ void up_schedule_sigaction(struct tcb_s *tcb)
    */
 
   tcb->xcp.saved_regs = tcb->xcp.regs;
+  tricore_sig_change_pprs(tcb, tricore_load_pprs(tcb));
 
   /* Create a new CSA for signal delivery. The new context
    * will borrow the process stack of the current tcb.
@@ -96,4 +97,5 @@ void up_schedule_sigaction(struct tcb_s *tcb)
                                     (up_getusrsp(tcb->xcp.regs)),
                                     PSW_IO_SUPERVISOR | PSW_CDE,
                                     true);
+  tricore_change_pprs(tcb, PSW_PRS_KERNEL_SET);
 }
diff --git a/arch/tricore/src/common/tricore_sigdeliver.c 
b/arch/tricore/src/common/tricore_sigdeliver.c
index c2e7e8c8e8c..f01db5af00d 100644
--- a/arch/tricore/src/common/tricore_sigdeliver.c
+++ b/arch/tricore/src/common/tricore_sigdeliver.c
@@ -117,5 +117,10 @@ retry:
   board_autoled_off(LED_SIGNAL);
 
   rtcb->xcp.regs = regs;
+  rtcb->xcp.saved_regs = NULL;
+
+  tricore_change_pprs(rtcb, tricore_sig_load_pprs(rtcb));
+  tricore_sig_change_pprs(rtcb, UINT32_MAX);
+
   tricore_fullcontextrestore();
 }
diff --git a/arch/tricore/src/common/tricore_svcall.c 
b/arch/tricore/src/common/tricore_svcall.c
index 9ee53e3e634..1f674e0eb30 100644
--- a/arch/tricore/src/common/tricore_svcall.c
+++ b/arch/tricore/src/common/tricore_svcall.c
@@ -110,6 +110,7 @@ void tricore_svcall(volatile void *trap)
   if (cmd != SYS_restore_context)
     {
       (*running_task)->xcp.regs = plregs;
+      tricore_store_pprs(*running_task);
     }
   else
     {
@@ -144,6 +145,8 @@ void tricore_svcall(volatile void *trap)
           puregs[REG_D8] = ret;
           rtcb->xcp.nsyscalls = index;
           rtcb->xcp.regs = plregs;
+          tricore_change_pprs(rtcb,
+                              tricore_syscall_load_pprs(rtcb, index));
           tricore_mtcr(TRICORE_CPU_PCXI,
                        tricore_addr2csa(rtcb->xcp.regs));
           UP_ISB();
@@ -192,6 +195,8 @@ void tricore_svcall(volatile void *trap)
           puregs[REG_PSW] =
             (puregs[REG_PSW] & (~PSW_MODE_MASK) & (~PSW_PRS_MASK)) |
             (PSW_IO_USER0 | PSW_PRS_USER);
+
+          tricore_change_pprs(tcb, PSW_PRS_USER_SET);
         }
         break;
 #endif
@@ -235,6 +240,8 @@ void tricore_svcall(volatile void *trap)
           puregs[REG_PSW] =
             (puregs[REG_PSW] & (~PSW_MODE_MASK) & (~PSW_PRS_MASK)) |
             (PSW_IO_USER0 | PSW_PRS_USER);
+
+          tricore_change_pprs(tcb, PSW_PRS_USER_SET);
         }
         break;
 #endif
@@ -283,6 +290,8 @@ void tricore_svcall(volatile void *trap)
           puregs[REG_PSW] =
             (puregs[REG_PSW] & (~PSW_MODE_MASK) & (~PSW_PRS_MASK)) |
             (PSW_IO_USER0 | PSW_PRS_USER);
+
+          tricore_change_pprs(rtcb, PSW_PRS_USER_SET);
         }
         break;
 #endif
@@ -309,6 +318,8 @@ void tricore_svcall(volatile void *trap)
           puregs[REG_PSW] =
             (puregs[REG_PSW] & (~PSW_MODE_MASK) & (~PSW_PRS_MASK)) |
             (PSW_IO_SUPERVISOR);
+
+          tricore_change_pprs(rtcb, PSW_PRS_KERNEL_SET);
         }
         break;
 #endif
@@ -328,6 +339,8 @@ void tricore_svcall(volatile void *trap)
 
           /* Setup to return to dispatch_syscall in privileged mode. */
 
+          tricore_syscall_store_pprs(rtcb, index);
+
           rtcb->xcp.syscall_regs[index] = plregs;
           rtcb->xcp.nsyscalls = index + 1;
 
@@ -341,6 +354,8 @@ void tricore_svcall(volatile void *trap)
             !(plregs[REG_LPCXI] & PCXI_PIE));
           puregs = tricore_csa2addr(plregs[REG_LPCXI]);
 
+          tricore_change_pprs(rtcb, PSW_PRS_KERNEL_SET);
+
           /* Args passed to dispatch_syscall */
 
           puregs[REG_UPCXI] = tricore_addr2csa(regs);
@@ -363,6 +378,19 @@ void tricore_svcall(volatile void *trap)
         break;
     }
 
+  /* Updata PPRS register */
+
+  tricore_restore_pprs(tcb);
+  UP_ISB();
+
+  /* (*running_task)->xcp.regs is about to become invalid and
+   * will be marked as NULL to avoid misusage. the same applies
+   * to (*running_task)->xcp.pprs.
+   */
+
+  (*running_task)->xcp.regs = NULL;
+  tricore_change_pprs(*running_task, UINT32_MAX);
+
   /* Return to the caller, tricore_svcall is entered by a jump */
 
   tricore_jump_backto_link();

Reply via email to