setjmp.S 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. /* Flush registers. */
  74. movi a4, __window_spill
  75. callx4 a4
  76. /* Copy the caller register a0-a3 at (sp - 16) to jmpbuf. */
  77. addi a7, a1, -16
  78. l32i a4, a7, 0
  79. l32i a5, a7, 4
  80. s32i a4, a2, 0
  81. s32i a5, a2, 4
  82. l32i a4, a7, 8
  83. l32i a5, a7, 12
  84. s32i a4, a2, 8
  85. s32i a5, a2, 12
  86. /* Copy the caller registers a4-a8/a12 from the overflow area. */
  87. /* Note that entry moved the SP by 16B, so SP of caller-1 is at 4(sp) */
  88. extui a7, a0, 30, 2
  89. blti a7, 2, .Lendsj
  90. l32i a8, a1, 4 /* a8: SP of 'caller-1' */
  91. slli a4, a7, 4
  92. sub a6, a8, a4
  93. addi a5, a2, 16
  94. addi a8, a8, -16 /* a8: end of register overflow area */
  95. .Lsjloop:
  96. l32i a7, a6, 0
  97. l32i a4, a6, 4
  98. s32i a7, a5, 0
  99. s32i a4, a5, 4
  100. l32i a7, a6, 8
  101. l32i a4, a6, 12
  102. s32i a7, a5, 8
  103. s32i a4, a5, 12
  104. addi a5, a6, 16
  105. addi a6, a6, 16
  106. blt a6, a8, .Lsjloop
  107. .Lendsj:
  108. /* Copy previous caller registers (this is assuming 'entry a1,16') */
  109. l32i a4, a1, 0
  110. l32i a5, a1, 4
  111. s32i a4, a2, 48
  112. s32i a5, a2, 52
  113. l32i a4, a1, 8
  114. l32i a5, a1, 12
  115. s32i a4, a2, 56
  116. s32i a5, a2, 60
  117. /* Save the return address, including the window size bits. */
  118. s32i a0, a2, 64
  119. /* a2 still points to jmp_buf. a3 contains savemask. */
  120. mov a6, a2
  121. mov a7, a3
  122. movi a3, __sigjmp_save
  123. callx4 a3
  124. mov a2, a6
  125. retw
  126. END(__sigsetjmp)
  127. weak_extern(_setjmp)
  128. weak_extern(setjmp)