setjmp.S 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* setjmp for Xtensa Processors.
  2. Copyright (C) 2001, 2007 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. /* This implementation relies heavily on the Xtensa register window
  16. mechanism. Setjmp flushes all the windows except its own to the
  17. stack and then copies registers from the save areas on the stack
  18. into the jmp_buf structure, along with the return address of the call
  19. to setjmp. Longjmp invalidates all the windows except its own, and
  20. then sets things up so that it will return to the right place,
  21. using a window underflow to automatically restore the registers.
  22. Note that we also save the area located just below the stack pointer
  23. of the caller. This save area could get overwritten by alloca
  24. following the call to setjmp. The alloca moves the stack pointer
  25. to allocate memory on the stack. This newly allocated memory
  26. includes(!) the original save area (alloca copies the save area
  27. before it moves that stack pointer).
  28. previous caller SP -> |------------------------------| <-----+
  29. | caller-2 registers a0-a3 | | p
  30. |------------------------------| | o
  31. | caller registers a4-a8/a12 | | i
  32. |------------------------------| | n
  33. | caller local stack | | t
  34. caller SP -> |------------------------------| <-+ | s
  35. | caller-1 registers a0-a3 | -:---+
  36. callee (setjmp) SP -> |==============================| |
  37. | caller registers a0-a3 | --+
  38. |------------------------------|
  39. In case of an alloca, registers a0-a3 of the previous caller (caller-1)
  40. are copied (*), and the original location get likely overwritten.
  41. previous caller SP -> |------------------------------| <-----+
  42. | caller-2 registers a0-a3 | | p
  43. |------------------------------| | o
  44. | caller registers a4-a8/a12 | | i
  45. |------------------------------| | n
  46. | caller local stack | | t
  47. caller SP before alloca-> |------------------------------| | s
  48. | alloca area (overwrites old | |
  49. | copy of caller-1 registers) | |
  50. caller SP after alloca -> |------------------------------| <-+ |
  51. | caller-1 registers a0-a3 (*) | -:---+
  52. callee (setjmp) SP -> |==============================| |
  53. | caller registers a0-a3 | --+
  54. |------------------------------|
  55. So, when longcall returns to the original caller SP, it also needs
  56. to restore the save area below the SP.
  57. */
  58. #include "sysdep.h"
  59. /* NOTE: The ENTRY macro must allocate exactly 16 bytes (entry a1, 16) */
  60. /* int setjmp (a2 = jmp_buf env) */
  61. ENTRY (_setjmp)
  62. movi a3, 0
  63. j 1f
  64. END (_setjmp)
  65. libc_hidden_def (_setjmp)
  66. ENTRY (setjmp)
  67. movi a3, 1
  68. j 1f
  69. END (setjmp)
  70. /* int __sigsetjmp (a2 = jmp_buf env, a3 = int savemask) */
  71. ENTRY (__sigsetjmp)
  72. 1:
  73. #if defined(__XTENSA_WINDOWED_ABI__)
  74. /* Flush registers. */
  75. movi a4, __window_spill
  76. callx4 a4
  77. /* Copy the caller register a0-a3 at (sp - 16) to jmpbuf. */
  78. addi a7, a1, -16
  79. l32i a4, a7, 0
  80. l32i a5, a7, 4
  81. s32i a4, a2, 0
  82. s32i a5, a2, 4
  83. l32i a4, a7, 8
  84. l32i a5, a7, 12
  85. s32i a4, a2, 8
  86. s32i a5, a2, 12
  87. /* Copy the caller registers a4-a8/a12 from the overflow area. */
  88. /* Note that entry moved the SP by 16B, so SP of caller-1 is at 4(sp) */
  89. extui a7, a0, 30, 2
  90. blti a7, 2, .Lendsj
  91. l32i a8, a1, 4 /* a8: SP of 'caller-1' */
  92. slli a4, a7, 4
  93. sub a6, a8, a4
  94. addi a5, a2, 16
  95. addi a8, a8, -16 /* a8: end of register overflow area */
  96. .Lsjloop:
  97. l32i a7, a6, 0
  98. l32i a4, a6, 4
  99. s32i a7, a5, 0
  100. s32i a4, a5, 4
  101. l32i a7, a6, 8
  102. l32i a4, a6, 12
  103. s32i a7, a5, 8
  104. s32i a4, a5, 12
  105. addi a5, a5, 16
  106. addi a6, a6, 16
  107. blt a6, a8, .Lsjloop
  108. .Lendsj:
  109. /* Copy previous caller registers (this is assuming 'entry a1,16') */
  110. l32i a4, a1, 0
  111. l32i a5, a1, 4
  112. s32i a4, a2, 48
  113. s32i a5, a2, 52
  114. l32i a4, a1, 8
  115. l32i a5, a1, 12
  116. s32i a4, a2, 56
  117. s32i a5, a2, 60
  118. /* Save the return address, including the window size bits. */
  119. s32i a0, a2, 64
  120. /* a2 still points to jmp_buf. a3 contains savemask. */
  121. mov a6, a2
  122. mov a7, a3
  123. movi a3, __sigjmp_save
  124. callx4 a3
  125. mov a2, a6
  126. retw
  127. #elif defined(__XTENSA_CALL0_ABI__)
  128. s32i a0, a2, 0
  129. s32i a1, a2, 4
  130. s32i a12, a2, 8
  131. s32i a13, a2, 12
  132. s32i a14, a2, 16
  133. s32i a15, a2, 20
  134. mov a12, a2
  135. movi a0, __sigjmp_save
  136. callx0 a0
  137. l32i a0, a12, 0
  138. l32i a12, a12, 8
  139. ret
  140. #else
  141. #error Unsupported Xtensa ABI
  142. #endif
  143. END(__sigsetjmp)
  144. weak_extern(_setjmp)
  145. weak_extern(setjmp)