nuttx/arch/xtensa/include
Tiago Medicci Serrano d3ffeb40a7 libc/machine/xtensa: make longjmp safe against context switch
In order to turn longjmp context-switch safe, it's necessary
to disable interrupts before modifying windowbase and windowstart.
Otherwise, after a context switch, windowstart and windowbase
would be different, leading to a wrongly set windowstart bit due to
longjmp writing it based on the windowbase before the context switch.
This corrupts the registers at the next window overflow reaching
that wrongly set bit.

*Background:*
This PR is related to an issue first observed on ESP-IDF
https://github.com/espressif/esp-idf/issues/5229 and it was, then,
checked on NuttX using a test application.

*The test application:*
To check if the problem affects ESP32, ESP32-S2 and ESP32-S3 on
NuttX, it was created an application based on:
https://en.cppreference.com/w/c/program/longjmp

The application creates 16 tasks (`#define NUMBER_OF_TASKS  16`)
that implements the following daemon:

```
static int setjmp_longjmp_daemon(int argc, char *argv[])
{
  for (int i = 0; i < NUMBER_OF_TASKS * 2; i++)
    {
      jmp_buf env;

      volatile int count = 0;
      if (setjmp(env) != UINT16_MAX)
        {
          foo(&env, ++count);
        }
    }

  sem_post(&g_sem);

  return EXIT_SUCCESS;
}
```

The main function also initializes a semaphore to avoid application
exiting before tasks return successfully:

```
  sem_init(&g_sem, 0, -NUMBER_OF_TASKS);
```

Finally, the round-robin interval was lowered to 1ms to raise the
chances of the longjmp being interrupted by a context switch
(`CONFIG_RR_INTERVAL=1).

This setup was able to reproduce the problem prior to this patch
being applied.
2022-11-22 19:34:44 +01:00
..
esp32 libc/machine/xtensa: make longjmp safe against context switch 2022-11-22 19:34:44 +01:00
esp32s2 libc/machine/xtensa: make longjmp safe against context switch 2022-11-22 19:34:44 +01:00
esp32s3 libc/machine/xtensa: make longjmp safe against context switch 2022-11-22 19:34:44 +01:00
lx6 arch: xtensa: Author Gregory Nutt: update licenses to Apache 2021-04-02 03:14:31 -05:00
lx7 xtensa: Add initial support for ESP32-S3 2022-01-27 13:46:50 -03:00
xtensa arch/xtensa: Refactor the differences in ABI calls. 2022-06-15 18:07:51 +03:00
.gitignore
arch.h arch: Move up_getsp from arch.h to irq.h 2022-07-01 10:44:55 -03:00
elf.h
inttypes.h arch: Omni Hoverboards: update licenses to Apache 2021-09-28 04:37:38 -07:00
irq.h xtensa: Define COMMON_CTX_REGS for chips without FPU (e.g. ESP32-S2) 2022-10-27 23:28:47 +08:00
limits.h arch: Define WCHAR_[MIN|MAX] in arch/include/limits.h 2022-06-03 22:25:49 +03:00
loadstore.h
setjmp.h xtensa: add setjmp.h include file 2021-11-17 02:23:45 -06:00
simcall.h
spinlock.h
stdarg.h arch:xtensa: add arch stdarg.h include file for xtensa 2021-08-09 17:58:25 -03:00
syscall.h xtensa: Add missing input operand on sys_call6 inline ASM 2022-05-18 15:46:57 +02:00
types.h arch: Add _wchar_t typedef like other basic types 2021-12-09 16:57:23 +09:00