esp_rom_longjmp.S 4.5 KB

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