esp-idf/components/esp_rom/patches/esp_rom_longjmp.S
Jakob Hasse a37c20b417 [system]: Made longjmp save for context switch
* Patched longjmp to be context-switch safe
  longjmp modifies the windowbase and windowstart
  registers, which isn't safe if a context switch
  occurs during the modification. After a context
  switch, windowstart and windowbase will 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.

  The solution is to disable interrupts during
  this code. It is only 6 instructions long,
  the impact shouldn't be significant.

  The fix is implemented as a wrapper which
  replaces the original first instructions of
  longjmp which are buggy. Then, it jumps back
  to execute the rest of the original longjmp
  function.

  Added a comparably reliable test to the
  test apps.
2021-05-11 12:04:27 +08:00

72 lines
2.7 KiB
ArmAsm

/*
Copyright (c) 2001-2006 by Tensilica Inc.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
This file contains a modified version of the original Xtensa longjmp implementation.
In this modified version, setting WINDOWSTART = 1 << WINDOWBASE is done inside a critical section.
This is necessary because after a FreeRTOS context switch in IDF, the values of WINDOWBASE and WINDOWSTART
are not guaranteed to be the same as before the context switch.
*/
#include <xtensa/corebits.h>
/*
Replacement of the first instructions of void longjmp (jmp_buf env, int val)
*/
.align 4
.literal_position
.global __wrap_longjmp
.type __wrap_longjmp, @function
__wrap_longjmp:
entry sp, 16
/* Deactivate interrupts in order to modify WINDOWBASE and WINDOWSTART. */
rsr a7, PS /* to be restored after SPILL_ALL_WINDOWS */
movi a5, PS_EXCM /* PS_INTLEVEL_MASK */
or a5, a7, a5 /* get the current INTLEVEL */
wsr a5, PS
/* Invalidate all but the current window;
set WindowStart to (1 << WindowBase). */
rsr a5, WINDOWBASE
movi a4, 1
ssl a5
sll a4, a4
wsr a4, WINDOWSTART
rsync
/* Activate interrupts again after modifying WINDOWBASE and WINDOWSTART. */
wsr a7, PS
/* Jump back to original longjmp implementation.
The jump target is the instrucion
l32i a0, a2, 64
of the original code. Hence, the original code's entry instruction and windowstart modification are left
out.
*/
movi a0, __real_longjmp + 20
jx a0
.size __wrap_longjmp, . - __wrap_longjmp