setjmp.S 4.0 KB

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