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

xiaoxiang 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 a095b1d6a6a arch: set (*running_task)->xcp.regs to NULL when exit from 
irq/exception
a095b1d6a6a is described below

commit a095b1d6a6adf560894b27e953e02908305922c8
Author: hujun5 <huj...@xiaomi.com>
AuthorDate: Wed Jan 22 19:14:15 2025 +0800

    arch: set (*running_task)->xcp.regs to NULL when exit from irq/exception
    
    reason:
    (*running_task)->xcp.regs is invalid when in threadcontext, we marke it as 
NULL to avoid misusage
    
    Signed-off-by: hujun5 <huj...@xiaomi.com>
---
 arch/arm/src/arm/arm_syscall.c                            |  9 ++++++++-
 arch/arm/src/armv6-m/arm_doirq.c                          |  5 +++++
 arch/arm/src/armv7-a/arm_doirq.c                          |  6 ++++++
 arch/arm/src/armv7-a/arm_syscall.c                        |  6 ++++++
 arch/arm/src/armv7-m/arm_doirq.c                          |  5 +++++
 arch/arm/src/armv7-r/arm_syscall.c                        |  6 ++++++
 arch/arm/src/armv8-m/arm_doirq.c                          |  5 +++++
 arch/arm/src/armv8-r/arm_syscall.c                        |  6 ++++++
 arch/arm64/src/common/arm64_doirq.c                       |  5 +++++
 arch/arm64/src/common/arm64_syscall.c                     |  9 ++++++++-
 arch/ceva/src/common/ceva_doirq.c                         |  6 ++++++
 arch/hc/src/common/hc_doirq.c                             |  6 ++++++
 arch/risc-v/src/common/riscv_doirq.c                      | 10 +++++++++-
 arch/risc-v/src/common/supervisor/riscv_perform_syscall.c |  9 ++++++++-
 arch/tricore/src/common/tricore_doirq.c                   |  5 +++++
 arch/x86_64/src/intel64/intel64_handlers.c                |  8 +++++++-
 arch/xtensa/src/common/xtensa_irqdispatch.c               | 10 +++++++++-
 17 files changed, 110 insertions(+), 6 deletions(-)

diff --git a/arch/arm/src/arm/arm_syscall.c b/arch/arm/src/arm/arm_syscall.c
index e5d87bb5b10..8dfafd04128 100644
--- a/arch/arm/src/arm/arm_syscall.c
+++ b/arch/arm/src/arm/arm_syscall.c
@@ -124,5 +124,12 @@ uint32_t *arm_syscall(uint32_t *regs)
    * SYS_context_switch system call.
    */
 
-  return tcb->xcp.regs;
+  regs = tcb->xcp.regs;
+
+  /* (*running_task)->xcp.regs is about to become invalid
+   * and will be marked as NULL to avoid misusage.
+   */
+
+  (*running_task)->xcp.regs = NULL;
+  return regs;
 }
diff --git a/arch/arm/src/armv6-m/arm_doirq.c b/arch/arm/src/armv6-m/arm_doirq.c
index 10fd627baac..f7891359b29 100644
--- a/arch/arm/src/armv6-m/arm_doirq.c
+++ b/arch/arm/src/armv6-m/arm_doirq.c
@@ -119,5 +119,10 @@ uint32_t *arm_doirq(int irq, uint32_t *regs)
 
   board_autoled_off(LED_INIRQ);
 
+  /* (*running_task)->xcp.regs is about to become invalid
+   * and will be marked as NULL to avoid misusage.
+   */
+
+  (*running_task)->xcp.regs = NULL;
   return regs;
 }
diff --git a/arch/arm/src/armv7-a/arm_doirq.c b/arch/arm/src/armv7-a/arm_doirq.c
index c73795bf92c..2de7bfcc02a 100644
--- a/arch/arm/src/armv7-a/arm_doirq.c
+++ b/arch/arm/src/armv7-a/arm_doirq.c
@@ -118,5 +118,11 @@ uint32_t *arm_doirq(int irq, uint32_t *regs)
 #endif
 
   board_autoled_off(LED_INIRQ);
+
+  /* (*running_task)->xcp.regs is about to become invalid
+   * and will be marked as NULL to avoid misusage.
+   */
+
+  tcb->xcp.regs = NULL;
   return regs;
 }
diff --git a/arch/arm/src/armv7-a/arm_syscall.c 
b/arch/arm/src/armv7-a/arm_syscall.c
index 84ce2f13f2a..1c024a4c18b 100644
--- a/arch/arm/src/armv7-a/arm_syscall.c
+++ b/arch/arm/src/armv7-a/arm_syscall.c
@@ -556,6 +556,12 @@ uint32_t *arm_syscall(uint32_t *regs)
 
   up_set_interrupt_context(false);
 
+  /* (*running_task)->xcp.regs is about to become invalid
+   * and will be marked as NULL to avoid misusage.
+   */
+
+  (*running_task)->xcp.regs = NULL;
+
   /* Return the last value of curent_regs.  This supports context switches
    * on return from the exception.  That capability is only used with the
    * SYS_context_switch system call.
diff --git a/arch/arm/src/armv7-m/arm_doirq.c b/arch/arm/src/armv7-m/arm_doirq.c
index 888fab96e54..d01f4341752 100644
--- a/arch/arm/src/armv7-m/arm_doirq.c
+++ b/arch/arm/src/armv7-m/arm_doirq.c
@@ -119,5 +119,10 @@ uint32_t *arm_doirq(int irq, uint32_t *regs)
 
   board_autoled_off(LED_INIRQ);
 
+  /* (*running_task)->xcp.regs is about to become invalid
+   * and will be marked as NULL to avoid misusage.
+   */
+
+  (*running_task)->xcp.regs = NULL;
   return regs;
 }
diff --git a/arch/arm/src/armv7-r/arm_syscall.c 
b/arch/arm/src/armv7-r/arm_syscall.c
index 713ccfe8f21..77fd6742dc6 100644
--- a/arch/arm/src/armv7-r/arm_syscall.c
+++ b/arch/arm/src/armv7-r/arm_syscall.c
@@ -553,6 +553,12 @@ uint32_t *arm_syscall(uint32_t *regs)
 
   up_set_interrupt_context(false);
 
+  /* (*running_task)->xcp.regs is about to become invalid
+   * and will be marked as NULL to avoid misusage.
+   */
+
+  (*running_task)->xcp.regs = NULL;
+
   /* Return the last value of curent_regs.  This supports context switches
    * on return from the exception.  That capability is only used with the
    * SYS_context_switch system call.
diff --git a/arch/arm/src/armv8-m/arm_doirq.c b/arch/arm/src/armv8-m/arm_doirq.c
index 4ae8ddac1b1..537c27d14ed 100644
--- a/arch/arm/src/armv8-m/arm_doirq.c
+++ b/arch/arm/src/armv8-m/arm_doirq.c
@@ -142,5 +142,10 @@ uint32_t *arm_doirq(int irq, uint32_t *regs)
     }
 #endif
 
+  /* (*running_task)->xcp.regs is about to become invalid
+   * and will be marked as NULL to avoid misusage.
+   */
+
+  (*running_task)->xcp.regs = NULL;
   return regs;
 }
diff --git a/arch/arm/src/armv8-r/arm_syscall.c 
b/arch/arm/src/armv8-r/arm_syscall.c
index af40f69960a..475d0a4f9b7 100644
--- a/arch/arm/src/armv8-r/arm_syscall.c
+++ b/arch/arm/src/armv8-r/arm_syscall.c
@@ -553,6 +553,12 @@ uint32_t *arm_syscall(uint32_t *regs)
 
   up_set_interrupt_context(false);
 
+  /* (*running_task)->xcp.regs is about to become invalid
+   * and will be marked as NULL to avoid misusage.
+   */
+
+  (*running_task)->xcp.regs = NULL;
+
   /* Return the last value of curent_regs.  This supports context switches
    * on return from the exception.  That capability is only used with the
    * SYS_context_switch system call.
diff --git a/arch/arm64/src/common/arm64_doirq.c 
b/arch/arm64/src/common/arm64_doirq.c
index 35482449707..ee470050092 100644
--- a/arch/arm64/src/common/arm64_doirq.c
+++ b/arch/arm64/src/common/arm64_doirq.c
@@ -115,6 +115,11 @@ uint64_t *arm64_doirq(int irq, uint64_t * regs)
 
   write_sysreg((uintptr_t)tcb & ~1ul, tpidr_el1);
 
+  /* (*running_task)->xcp.regs is about to become invalid
+   * and will be marked as NULL to avoid misusage.
+   */
+
+  tcb->xcp.regs = NULL;
   return regs;
 }
 
diff --git a/arch/arm64/src/common/arm64_syscall.c 
b/arch/arm64/src/common/arm64_syscall.c
index 310c6d99dfa..3bd6e12bcb8 100644
--- a/arch/arm64/src/common/arm64_syscall.c
+++ b/arch/arm64/src/common/arm64_syscall.c
@@ -324,5 +324,12 @@ uint64_t *arm64_syscall(uint64_t *regs)
         break;
     }
 
-  return tcb->xcp.regs;
+  regs = tcb->xcp.regs;
+
+  /* (*running_task)->xcp.regs is about to become invalid
+   * and will be marked as NULL to avoid misusage.
+   */
+
+  (*running_task)->xcp.regs = NULL;
+  return regs;
 }
diff --git a/arch/ceva/src/common/ceva_doirq.c 
b/arch/ceva/src/common/ceva_doirq.c
index 1c3c6e8dd03..b5bf60ef942 100644
--- a/arch/ceva/src/common/ceva_doirq.c
+++ b/arch/ceva/src/common/ceva_doirq.c
@@ -119,6 +119,12 @@ uint32_t *ceva_doirq(int irq, uint32_t *regs)
           memcpy((uint32_t *)regs[REG_SP], regs, XCPTCONTEXT_SIZE);
           regs = (uint32_t *)regs[REG_SP];
         }
+
+      /* (*running_task)->xcp.regs is about to become invalid
+       * and will be marked as NULL to avoid misusage.
+       */
+
+      (*running_task)->xcp.regs = NULL;
     }
 
   return regs;
diff --git a/arch/hc/src/common/hc_doirq.c b/arch/hc/src/common/hc_doirq.c
index 01cc5cb7b74..cfeba105bda 100644
--- a/arch/hc/src/common/hc_doirq.c
+++ b/arch/hc/src/common/hc_doirq.c
@@ -132,5 +132,11 @@ uint8_t *hc_doirq(int irq, uint8_t *regs)
   up_set_current_regs(NULL);
 #endif
   board_autoled_off(LED_INIRQ);
+
+  /* (*running_task)->xcp.regs is about to become invalid
+   * and will be marked as NULL to avoid misusage.
+   */
+
+  (*running_task)->xcp.regs = NULL;
   return regs;
 }
diff --git a/arch/risc-v/src/common/riscv_doirq.c 
b/arch/risc-v/src/common/riscv_doirq.c
index 4325bdad50f..734087a5c57 100644
--- a/arch/risc-v/src/common/riscv_doirq.c
+++ b/arch/risc-v/src/common/riscv_doirq.c
@@ -138,5 +138,13 @@ uintreg_t *riscv_doirq(int irq, uintreg_t *regs)
 
 #endif
   board_autoled_off(LED_INIRQ);
-  return tcb->xcp.regs;
+
+  regs = tcb->xcp.regs;
+
+  /* (*running_task)->xcp.regs is about to become invalid
+   * and will be marked as NULL to avoid misusage.
+   */
+
+  (*running_task)->xcp.regs = NULL;
+  return regs;
 }
diff --git a/arch/risc-v/src/common/supervisor/riscv_perform_syscall.c 
b/arch/risc-v/src/common/supervisor/riscv_perform_syscall.c
index cb4b6c43337..c1afb5cc086 100644
--- a/arch/risc-v/src/common/supervisor/riscv_perform_syscall.c
+++ b/arch/risc-v/src/common/supervisor/riscv_perform_syscall.c
@@ -89,5 +89,12 @@ void *riscv_perform_syscall(uintreg_t *regs)
 
   up_set_interrupt_context(false);
 
-  return tcb->xcp.regs;
+  regs = tcb->xcp.regs;
+
+  /* (*running_task)->xcp.regs is about to become invalid
+   * and will be marked as NULL to avoid misusage.
+   */
+
+  (*running_task)->xcp.regs = NULL;
+  return regs;
 }
diff --git a/arch/tricore/src/common/tricore_doirq.c 
b/arch/tricore/src/common/tricore_doirq.c
index f510f355466..3f83c6ef412 100644
--- a/arch/tricore/src/common/tricore_doirq.c
+++ b/arch/tricore/src/common/tricore_doirq.c
@@ -117,6 +117,11 @@ IFX_INTERRUPT_INTERNAL(tricore_doirq, 0, 255)
 
   up_set_current_regs(NULL);
 
+  /* (*running_task)->xcp.regs is about to become invalid
+   * and will be marked as NULL to avoid misusage.
+   */
+
+  (*running_task)->xcp.regs = NULL;
   board_autoled_off(LED_INIRQ);
 #endif
 }
diff --git a/arch/x86_64/src/intel64/intel64_handlers.c 
b/arch/x86_64/src/intel64/intel64_handlers.c
index 2e3d1820ede..24a04574f78 100644
--- a/arch/x86_64/src/intel64/intel64_handlers.c
+++ b/arch/x86_64/src/intel64/intel64_handlers.c
@@ -126,8 +126,14 @@ static uint64_t *common_handler(int irq, uint64_t *regs)
   /* Clear irq flag */
 
   up_set_interrupt_context(false);
+  regs = tcb->xcp.regs;
 
-  return tcb->xcp.regs;
+  /* (*running_task)->xcp.regs is about to become invalid
+   * and will be marked as NULL to avoid misusage.
+   */
+
+  (*running_task)->xcp.regs = NULL;
+  return regs;
 }
 #endif
 
diff --git a/arch/xtensa/src/common/xtensa_irqdispatch.c 
b/arch/xtensa/src/common/xtensa_irqdispatch.c
index 0b3b26d17b7..54a8ebb2ce3 100644
--- a/arch/xtensa/src/common/xtensa_irqdispatch.c
+++ b/arch/xtensa/src/common/xtensa_irqdispatch.c
@@ -114,5 +114,13 @@ uint32_t *xtensa_irq_dispatch(int irq, uint32_t *regs)
 #endif
 
   board_autoled_off(LED_INIRQ);
-  return tcb->xcp.regs;
+
+  regs = tcb->xcp.regs;
+
+  /* (*running_task)->xcp.regs is about to become invalid
+   * and will be marked as NULL to avoid misusage.
+   */
+
+  (*running_task)->xcp.regs = NULL;
+  return regs;
 }

Reply via email to