setjmp.S 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 it would probably be sufficient to only copy the
  23. registers from setjmp's caller into jmp_buf. However, we also copy
  24. the save area located at the stack pointer of setjmp's caller.
  25. This save area will typically remain intact until the longjmp call.
  26. The one exception is when there is an intervening alloca in
  27. setjmp's caller. This is certainly an unusual situation and is
  28. likely to cause problems in any case (the storage allocated on the
  29. stack cannot be safely accessed following the longjmp). As bad as
  30. it is, on most systems this situation would not necessarily lead to
  31. a catastrophic failure. If we did not preserve the extra save area
  32. on Xtensa, however, it would. When setjmp's caller returns after a
  33. longjmp, there will be a window underflow; an invalid return
  34. address or stack pointer in the save area will almost certainly
  35. lead to a crash. Keeping a copy of the extra save area in the
  36. jmp_buf avoids this with only a small additional cost. If setjmp
  37. and longjmp are ever time-critical, this could be removed. */
  38. #include "sysdep.h"
  39. /* int setjmp (a2 = jmp_buf env) */
  40. ENTRY (_setjmp)
  41. movi a3, 0
  42. j 1f
  43. END (_setjmp)
  44. libc_hidden_def (_setjmp)
  45. ENTRY (setjmp)
  46. movi a3, 1
  47. j 1f
  48. END (setjmp)
  49. /* int __sigsetjmp (a2 = jmp_buf env,
  50. a3 = int savemask) */
  51. ENTRY (__sigsetjmp)
  52. 1:
  53. /* Flush registers. */
  54. movi a4, __window_spill
  55. callx4 a4
  56. /* Preserve the second argument (savemask) in a15. The selection
  57. of a15 is arbitrary, except it's otherwise unused. There is no
  58. risk of triggering a window overflow since we just returned
  59. from __window_spill(). */
  60. mov a15, a3
  61. /* Copy the register save area at (sp - 16). */
  62. addi a5, a1, -16
  63. l32i a3, a5, 0
  64. l32i a4, a5, 4
  65. s32i a3, a2, 0
  66. s32i a4, a2, 4
  67. l32i a3, a5, 8
  68. l32i a4, a5, 12
  69. s32i a3, a2, 8
  70. s32i a4, a2, 12
  71. /* Copy 0-8 words from the register overflow area. */
  72. extui a3, a0, 30, 2
  73. blti a3, 2, .Lendsj
  74. l32i a7, a5, 4
  75. slli a4, a3, 4
  76. sub a5, a7, a4
  77. addi a6, a2, 16
  78. addi a7, a7, -16 /* a7 = end of register overflow area */
  79. .Lsjloop:
  80. l32i a3, a5, 0
  81. l32i a4, a5, 4
  82. s32i a3, a6, 0
  83. s32i a4, a6, 4
  84. l32i a3, a5, 8
  85. l32i a4, a5, 12
  86. s32i a3, a6, 8
  87. s32i a4, a6, 12
  88. addi a5, a5, 16
  89. addi a6, a6, 16
  90. blt a5, a7, .Lsjloop
  91. .Lendsj:
  92. /* Copy the register save area at sp. */
  93. l32i a3, a1, 0
  94. l32i a4, a1, 4
  95. s32i a3, a2, 48
  96. s32i a4, a2, 52
  97. l32i a3, a1, 8
  98. l32i a4, a1, 12
  99. s32i a3, a2, 56
  100. s32i a4, a2, 60
  101. /* Save the return address, including the window size bits. */
  102. s32i a0, a2, 64
  103. /* a2 still addresses jmp_buf. a15 contains savemask. */
  104. mov a6, a2
  105. mov a7, a15
  106. movi a3, __sigjmp_save
  107. callx4 a3
  108. mov a2, a6
  109. retw
  110. END(__sigsetjmp)
  111. weak_extern(_setjmp)
  112. weak_extern(setjmp)