1/* 2 Copyright (c) 2001-2006 by Tensilica Inc. 3 4 Permission is hereby granted, free of charge, to any person obtaining 5 a copy of this software and associated documentation files (the 6 "Software"), to deal in the Software without restriction, including 7 without limitation the rights to use, copy, modify, merge, publish, 8 distribute, sublicense, and/or sell copies of the Software, and to 9 permit persons to whom the Software is furnished to do so, subject to 10 the following conditions: 11 12 The above copyright notice and this permission notice shall be included 13 in all copies or substantial portions of the Software. 14 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 19 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 20 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 21 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22*/ 23 24/* 25 This file contains a modified version of the original Xtensa longjmp implementation. 26 In this modified version, setting WINDOWSTART = 1 << WINDOWBASE is done inside a critical section. 27 This is necessary because after a FreeRTOS context switch in IDF, the values of WINDOWBASE and WINDOWSTART 28 are not guaranteed to be the same as before the context switch. 29*/ 30 31#include <xtensa/corebits.h> 32#include <sdkconfig.h> 33 34/* void longjmp (jmp_buf env, int val) */ 35 36 .align 4 37 .literal_position 38 .global __wrap_longjmp 39 .type __wrap_longjmp, @function 40__wrap_longjmp: 41 entry sp, 16 42 43 /* Deactivate interrupts in order to modify WINDOWBASE and WINDOWSTART. */ 44 rsr a7, PS /* to be restored after SPILL_ALL_WINDOWS */ 45 movi a5, PS_EXCM /* PS_INTLEVEL_MASK */ 46 or a5, a7, a5 /* get the current INTLEVEL */ 47 wsr a5, PS 48 49 /* Invalidate all but the current window; 50 set WindowStart to (1 << WindowBase). */ 51 rsr a5, WINDOWBASE 52 movi a4, 1 53 ssl a5 54 sll a4, a4 55 wsr a4, WINDOWSTART 56 rsync 57 58 /* Activate interrupts again after modifying WINDOWBASE and WINDOWSTART. */ 59 wsr a7, PS 60 61#if !CONFIG_IDF_TARGET_ESP32S3 62 63 /* 64 If not on S3, replacement of only the first instructions, 65 then jump back to original longjmp implementation. 66 The jump target is the instrucion 67 l32i a0, a2, 64 68 of the original code. Hence, the original code's entry instruction and windowstart modification are left 69 out. 70 */ 71 movi a0, __real_longjmp + 20 72 jx a0 73 74 .size __wrap_longjmp, . - __wrap_longjmp 75 76#else /* CONFIG_IDF_TARGET_ESP32S3 */ 77 /* 78 If on S3, we replace the whole function for simplicity. The placement of longjmp in ROM is ECO-dependent 79 on S3. 80 */ 81 82 /* 83 Return to the return address of the setjmp, using the 84 window size bits from the setjmp call so that the caller 85 will be able to find the return value that we put in a2. */ 86 87 l32i a0, a2, 64 88 89 /* Copy the first 4 saved registers from jmp_buf into the save area 90 at the current sp so that the values will be restored to registers 91 when longjmp returns. */ 92 93 addi a7, a1, -16 94 l32i a4, a2, 0 95 l32i a5, a2, 4 96 s32i a4, a7, 0 97 s32i a5, a7, 4 98 l32i a4, a2, 8 99 l32i a5, a2, 12 100 s32i a4, a7, 8 101 s32i a5, a7, 12 102 103 /* Copy the remaining 0-8 saved registers. */ 104 extui a7, a0, 30, 2 105 blti a7, 2, .Lendlj 106 l32i a8, a2, 52 107 slli a4, a7, 4 108 sub a6, a8, a4 109 addi a5, a2, 16 110 addi a8, a8, -16 // a8 = end of register overflow area 111.Lljloop: 112 l32i a7, a5, 0 113 l32i a4, a5, 4 114 s32i a7, a6, 0 115 s32i a4, a6, 4 116 l32i a7, a5, 8 117 l32i a4, a5, 12 118 s32i a7, a6, 8 119 s32i a4, a6, 12 120 addi a5, a5, 16 121 addi a6, a6, 16 122 blt a6, a8, .Lljloop 123.Lendlj: 124 125 /* The 4 words saved from the register save area at the target's 126 sp are copied back to the target procedure's save area. The 127 only point of this is to prevent a catastrophic failure in 128 case the contents were moved by an alloca after calling 129 setjmp. This is a bit paranoid but it doesn't cost much. */ 130 131 l32i a7, a2, 4 // load the target stack pointer 132 addi a7, a7, -16 // find the destination save area 133 l32i a4, a2, 48 134 l32i a5, a2, 52 135 s32i a4, a7, 0 136 s32i a5, a7, 4 137 l32i a4, a2, 56 138 l32i a5, a2, 60 139 s32i a4, a7, 8 140 s32i a5, a7, 12 141 142 /* Return val ? val : 1. */ 143 movi a2, 1 144 movnez a2, a3, a3 145 146 retw 147 .size __wrap_longjmp, . - __wrap_longjmp 148 149#endif /* CONFIG_IDF_TARGET_ESP32S3 */ 150