__longjmp.S 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* longjmp 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. ENTRY (__longjmp)
  40. /* Invalidate all but the current window. Reading and writing
  41. special registers WINDOWBASE and WINDOWSTART are
  42. privileged operations, so user processes must call the
  43. slower __window_spill() to do the job. */
  44. movi a4, __window_spill
  45. callx4 a4
  46. /* Return to the return address of the setjmp, using the
  47. window size bits from the setjmp call so that the caller
  48. will be able to find the return value that we put in a2. */
  49. l32i a0, a2, 64
  50. /* Copy the first 4 saved registers from jmp_buf into the save area
  51. at the current sp so that the values will be restored to registers
  52. when longjmp returns. */
  53. addi a7, a1, -16
  54. l32i a4, a2, 0
  55. l32i a5, a2, 4
  56. s32i a4, a7, 0
  57. s32i a5, a7, 4
  58. l32i a4, a2, 8
  59. l32i a5, a2, 12
  60. s32i a4, a7, 8
  61. s32i a5, a7, 12
  62. /* Copy the remaining 0-8 saved registers. */
  63. extui a7, a0, 30, 2
  64. blti a7, 2, .Lendlj
  65. l32i a8, a2, 52
  66. slli a4, a7, 4
  67. sub a6, a8, a4
  68. addi a5, a2, 16
  69. addi a8, a8, -16 /* a8 = end of register overflow area */
  70. .Lljloop:
  71. l32i a7, a5, 0
  72. l32i a4, a5, 4
  73. s32i a7, a6, 0
  74. s32i a4, a6, 4
  75. l32i a7, a5, 8
  76. l32i a4, a5, 12
  77. s32i a7, a6, 8
  78. s32i a4, a6, 12
  79. addi a5, a5, 16
  80. addi a6, a6, 16
  81. blt a6, a8, .Lljloop
  82. .Lendlj:
  83. /* The 4 words saved from the register save area at the target's
  84. sp are copied back to the target procedure's save area. The
  85. only point of this is to prevent a catastrophic failure in
  86. case the contents were moved by an alloca after calling
  87. setjmp. This is a bit paranoid but it doesn't cost much. */
  88. l32i a7, a2, 4 /* load the target stack pointer */
  89. addi a7, a7, -16 /* find the destination save area */
  90. l32i a4, a2, 48
  91. l32i a5, a2, 52
  92. s32i a4, a7, 0
  93. s32i a5, a7, 4
  94. l32i a4, a2, 56
  95. l32i a5, a2, 60
  96. s32i a4, a7, 8
  97. s32i a5, a7, 12
  98. /* Return v ? v : 1. */
  99. movi a2, 1
  100. movnez a2, a3, a3
  101. retw
  102. END (__longjmp)
  103. libc_hidden_def (__longjmp)