Skip to content

Commit

Permalink
Based on a change recommended by Mark Shulte:
Browse files Browse the repository at this point in the history
Signal handlers maybe run with interrupts enabled or disabled, depending on how the task the received the signal was blocked. (i.e.: If sem_wait() is called, then we disable interrupts, then block the currently running task). This could be dangerous, because user code would be running with interrupts disabled.

This change forces interrupts to be enabled in up_sigdeliver() before executing the signal handler calling up_irq_enable() explicitly.  This is safe because, when we return to normal execution, interrupts will be restored to their previous state when the signal handler returns.
  • Loading branch information
gregory-nutt committed Jun 6, 2018
1 parent 9222f50 commit 977d41d
Show file tree
Hide file tree
Showing 20 changed files with 134 additions and 108 deletions.
2 changes: 1 addition & 1 deletion arch/arm/include/armv7-m/irq.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ static inline irqstate_t up_irq_save(void)
static inline void up_irq_enable(void) inline_function;
static inline void up_irq_enable(void)
{
setbasepri(0);
setbasepri(NVIC_SYSH_PRIORITY_MIN);
__asm__ __volatile__ ("\tcpsie i\n");
}

Expand Down
10 changes: 7 additions & 3 deletions arch/arm/src/arm/up_sigdeliver.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/****************************************************************************
* arch/arm/src/arm/up_sigdeliver.c
*
* Copyright (C) 2007-2010, 2015 Gregory Nutt. All rights reserved.
* Copyright (C) 2007-2010, 2015, 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -113,9 +113,13 @@ void up_sigdeliver(void)
sigdeliver = rtcb->xcp.sigdeliver;
rtcb->xcp.sigdeliver = NULL;

/* Then restore the task interrupt state */
#ifndef CONFIG_SUPPRESS_INTERRUPTS
/* Then make sure that interrupts are enabled. Signal handlers must always
* run with interrupts enabled.
*/

up_irq_restore(regs[REG_CPSR]);
up_irq_enable();
#endif

/* Deliver the signals */

Expand Down
22 changes: 7 additions & 15 deletions arch/arm/src/armv6-m/up_sigdeliver.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/****************************************************************************
* arch/arm/src/armv6-m/up_sigdeliver.c
*
* Copyright (C) 2013-2015 Gregory Nutt. All rights reserved.
* Copyright (C) 2013-2015, 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -54,18 +54,6 @@

#ifndef CONFIG_DISABLE_SIGNALS

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

/****************************************************************************
* Private Data
****************************************************************************/

/****************************************************************************
* Private Functions
****************************************************************************/

/****************************************************************************
* Public Functions
****************************************************************************/
Expand Down Expand Up @@ -122,9 +110,13 @@ void up_sigdeliver(void)
sigdeliver = rtcb->xcp.sigdeliver;
rtcb->xcp.sigdeliver = NULL;

/* Then restore the task interrupt state */
#ifndef CONFIG_SUPPRESS_INTERRUPTS
/* Then make sure that interrupts are enabled. Signal handlers must always
* run with interrupts enabled.
*/

up_irq_restore((uint8_t)regs[REG_PRIMASK]);
up_irq_enable();
#endif

/* Deliver the signal */

Expand Down
10 changes: 7 additions & 3 deletions arch/arm/src/armv7-a/arm_sigdeliver.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/****************************************************************************
* arch/arm/src/armv7-a/arm_sigdeliver.c
*
* Copyright (C) 2013, 2015-2016 Gregory Nutt. All rights reserved.
* Copyright (C) 2013, 2015-2016, 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -101,9 +101,13 @@ void up_sigdeliver(void)
sigdeliver = rtcb->xcp.sigdeliver;
rtcb->xcp.sigdeliver = NULL;

/* Then restore the task interrupt state */
#ifndef CONFIG_SUPPRESS_INTERRUPTS
/* Then make sure that interrupts are enabled. Signal handlers must always
* run with interrupts enabled.
*/

leave_critical_section(regs[REG_CPSR]);
up_irq_enable();
#endif

/* Deliver the signal */

Expand Down
13 changes: 7 additions & 6 deletions arch/arm/src/armv7-m/up_sigdeliver.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/****************************************************************************
* arch/arm/src/armv7-m/up_sigdeliver.c
*
* Copyright (C) 2009-2010, 2013-2016 Gregory Nutt. All rights reserved.
* Copyright (C) 2009-2010, 2013-2016, 2018 Gregory Nutt. All rights
* reserved.
* Author: Gregory Nutt <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -109,12 +110,12 @@ void up_sigdeliver(void)
sigdeliver = (sig_deliver_t)rtcb->xcp.sigdeliver;
rtcb->xcp.sigdeliver = NULL;

/* Then restore the task interrupt state */
#ifndef CONFIG_SUPPRESS_INTERRUPTS
/* Then make sure that interrupts are enabled. Signal handlers must always
* run with interrupts enabled.
*/

#ifdef CONFIG_ARMV7M_USEBASEPRI
leave_critical_section((uint8_t)regs[REG_BASEPRI]);
#else
leave_critical_section((uint16_t)regs[REG_PRIMASK]);
up_irq_enable();
#endif

/* Deliver the signal */
Expand Down
10 changes: 7 additions & 3 deletions arch/arm/src/armv7-r/arm_sigdeliver.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/****************************************************************************
* arch/arm/src/armv7-r/arm_sigdeliver.c
*
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
* Copyright (C) 2015, 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -101,9 +101,13 @@ void up_sigdeliver(void)
sigdeliver = rtcb->xcp.sigdeliver;
rtcb->xcp.sigdeliver = NULL;

/* Then restore the task interrupt state */
#ifndef CONFIG_SUPPRESS_INTERRUPTS
/* Then make sure that interrupts are enabled. Signal handlers must always
* run with interrupts enabled.
*/

up_irq_restore(regs[REG_CPSR]);
up_irq_enable();
#endif

/* Deliver the signals */

Expand Down
10 changes: 7 additions & 3 deletions arch/avr/src/avr/up_sigdeliver.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/****************************************************************************
* arch/avr/src/avr/up_sigdeliver.c
*
* Copyright (C) 2011, 2015 Gregory Nutt. All rights reserved.
* Copyright (C) 2011, 2015, 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -104,9 +104,13 @@ void up_sigdeliver(void)
sigdeliver = rtcb->xcp.sigdeliver;
rtcb->xcp.sigdeliver = NULL;

/* Then restore the task interrupt state */
#ifndef CONFIG_SUPPRESS_INTERRUPTS
/* Then make sure that interrupts are enabled. Signal handlers must always
* run with interrupts enabled.
*/

up_irq_restore(regs[REG_SREG]);
up_irq_enable();
#endif

/* Deliver the signals */

Expand Down
10 changes: 7 additions & 3 deletions arch/avr/src/avr32/up_sigdeliver.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/****************************************************************************
* arch/avr/src/avr32/up_sigdeliver.c
*
* Copyright (C) 2010, 2015 Gregory Nutt. All rights reserved.
* Copyright (C) 2010, 2015, 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -104,9 +104,13 @@ void up_sigdeliver(void)
sigdeliver = rtcb->xcp.sigdeliver;
rtcb->xcp.sigdeliver = NULL;

/* Then restore the task interrupt state */
#ifndef CONFIG_SUPPRESS_INTERRUPTS
/* Then make sure that interrupts are enabled. Signal handlers must always
* run with interrupts enabled.
*/

up_irq_restore(regs[REG_SR]);
up_irq_enable();
#endif

/* Deliver the signals */

Expand Down
10 changes: 7 additions & 3 deletions arch/mips/src/mips32/up_sigdeliver.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/****************************************************************************
* arch/mips/src/mips32/up_sigdeliver.c
*
* Copyright (C) 2011, 2015 Gregory Nutt. All rights reserved.
* Copyright (C) 2011, 2015, 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -114,9 +114,13 @@ void up_sigdeliver(void)
sigdeliver = rtcb->xcp.sigdeliver;
rtcb->xcp.sigdeliver = NULL;

/* Then restore the task interrupt state */
#ifndef CONFIG_SUPPRESS_INTERRUPTS
/* Then make sure that interrupts are enabled. Signal handlers must always
* run with interrupts enabled.
*/

up_irq_restore((irqstate_t)regs[REG_STATUS]);
up_irq_enable();
#endif

/* Deliver the signals */

Expand Down
10 changes: 7 additions & 3 deletions arch/misoc/src/lm32/lm32_sigdeliver.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/****************************************************************************
* arch/misoc/src/lm32/lm32_sigdeliver.c
*
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
* Copyright (C) 2016, 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -101,9 +101,13 @@ void lm32_sigdeliver(void)
sigdeliver = rtcb->xcp.sigdeliver;
rtcb->xcp.sigdeliver = NULL;

/* Then restore the task interrupt state */
#ifndef CONFIG_SUPPRESS_INTERRUPTS
/* Then make sure that interrupts are enabled. Signal handlers must always
* run with interrupts enabled.
*/

up_irq_restore((irqstate_t)regs[REG_INT_CTX]);
up_irq_enable();
#endif

/* Deliver the signals */

Expand Down
10 changes: 7 additions & 3 deletions arch/renesas/src/m16c/m16c_sigdeliver.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/****************************************************************************
* arch/renesas/src/m16c/m16c_sigdeliver.c
*
* Copyright (C) 2009-2010, 2015 Gregory Nutt. All rights reserved.
* Copyright (C) 2009-2010, 2015, 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -115,9 +115,13 @@ void up_sigdeliver(void)
sigdeliver = rtcb->xcp.sigdeliver;
rtcb->xcp.sigdeliver = NULL;

/* Then restore the task interrupt state. */
#ifndef CONFIG_SUPPRESS_INTERRUPTS
/* Then make sure that interrupts are enabled. Signal handlers must always
* run with interrupts enabled.
*/

up_irq_restore(rtcb->xcp.saved_flg);
up_irq_enable();
#endif

/* Deliver the signals */

Expand Down
10 changes: 7 additions & 3 deletions arch/renesas/src/sh1/sh1_sigdeliver.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/****************************************************************************
* arch/renesas/src/common/up_sigdeliver.c
*
* Copyright (C) 2008-2010, 2015 Gregory Nutt. All rights reserved.
* Copyright (C) 2008-2010, 2015, 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -114,9 +114,13 @@ void up_sigdeliver(void)
sigdeliver = rtcb->xcp.sigdeliver;
rtcb->xcp.sigdeliver = NULL;

/* Then restore the task interrupt state. */
#ifndef CONFIG_SUPPRESS_INTERRUPTS
/* Then make sure that interrupts are enabled. Signal handlers must always
* run with interrupts enabled.
*/

up_irq_restore(regs[REG_SR] & 0x000000f0);
up_irq_enable();
#endif

/* Deliver the signals */

Expand Down
22 changes: 7 additions & 15 deletions arch/risc-v/src/rv32im/up_sigdeliver.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/****************************************************************************
* arch/risc-v/src/rv32im/up_sigdeliver.c
*
* Copyright (C) 2011, 2015 Gregory Nutt. All rights reserved.
* Copyright (C) 2011, 2015, 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <[email protected]>
*
* Modified for RISC-V:
Expand Down Expand Up @@ -60,18 +60,6 @@

#ifndef CONFIG_DISABLE_SIGNALS

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

/****************************************************************************
* Private Data
****************************************************************************/

/****************************************************************************
* Private Functions
****************************************************************************/

/****************************************************************************
* Public Functions
****************************************************************************/
Expand Down Expand Up @@ -119,9 +107,13 @@ void up_sigdeliver(void)
sigdeliver = rtcb->xcp.sigdeliver;
rtcb->xcp.sigdeliver = NULL;

/* Then restore the task interrupt state */
#ifndef CONFIG_SUPPRESS_INTERRUPTS
/* Then make sure that interrupts are enabled. Signal handlers must always
* run with interrupts enabled.
*/

up_irq_restore((irqstate_t)regs[REG_INT_CTX]);
up_irq_enable();
#endif

/* Deliver the signals */

Expand Down
22 changes: 7 additions & 15 deletions arch/x86/src/i486/up_sigdeliver.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/****************************************************************************
* arch/x86/src/i486/up_sigdeliver.c
*
* Copyright (C) 2011, 2015 Gregory Nutt. All rights reserved.
* Copyright (C) 2011, 2015, 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -54,18 +54,6 @@

#ifndef CONFIG_DISABLE_SIGNALS

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

/****************************************************************************
* Private Data
****************************************************************************/

/****************************************************************************
* Private Functions
****************************************************************************/

/****************************************************************************
* Public Functions
****************************************************************************/
Expand Down Expand Up @@ -113,9 +101,13 @@ void up_sigdeliver(void)
sigdeliver = rtcb->xcp.sigdeliver;
rtcb->xcp.sigdeliver = NULL;

/* Then restore the task interrupt state */
#ifndef CONFIG_SUPPRESS_INTERRUPTS
/* Then make sure that interrupts are enabled. Signal handlers must always
* run with interrupts enabled.
*/

up_irq_restore(regs[REG_EFLAGS]);
up_irq_enable();
#endif

/* Deliver the signals */

Expand Down
Loading

0 comments on commit 977d41d

Please sign in to comment.