swapcontext.S 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Save and set current context.
  2. Copyright (C) 2009 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Maciej W. Rozycki <macro@codesourcery.com>.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library. If not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <sysdep.h>
  17. #include <sys/asm.h>
  18. #include <sys/fpregdef.h>
  19. #include <sys/regdef.h>
  20. #include "ucontext_i.h"
  21. /* int swapcontext (ucontext_t *oucp, const ucontext_t *ucp) */
  22. .text
  23. LOCALSZ = 0
  24. ARGSZ = 0
  25. MASK = 0x00000000
  26. #ifdef __PIC__
  27. LOCALSZ = 1 /* save gp */
  28. #endif
  29. #if _MIPS_SIM != _ABIO32
  30. ARGSZ = 1 /* save a1 */
  31. # ifdef __PIC__
  32. MASK = 0x10000000
  33. # endif
  34. #endif
  35. FRAMESZ = (((ARGSZ + LOCALSZ) * SZREG) + ALSZ) & ALMASK
  36. GPOFF = FRAMESZ - ((ARGSZ + 1) * SZREG)
  37. #if _MIPS_SIM != _ABIO32
  38. A1OFF = FRAMESZ - (1 * SZREG) /* callee-allocated */
  39. #else
  40. A1OFF = FRAMESZ + (1 * SZREG) /* caller-allocated */
  41. #endif
  42. NESTED (__swapcontext, FRAMESZ, ra)
  43. .mask MASK, -(ARGSZ * SZREG)
  44. .fmask 0x00000000, 0
  45. #ifdef __PIC__
  46. SETUP_GP
  47. move a2, sp
  48. # define _SP a2
  49. # if _MIPS_SIM != _ABIO32
  50. move a3, gp
  51. # define _GP a3
  52. # endif
  53. PTR_ADDIU sp, -FRAMESZ
  54. SETUP_GP64 (GPOFF, __swapcontext)
  55. SAVE_GP (GPOFF)
  56. #else /* ! __PIC__ */
  57. # define _SP sp
  58. # define _GP gp
  59. #endif /* ! __PIC__ */
  60. #ifdef PROF
  61. .set noat
  62. move AT, ra
  63. jal _mcount
  64. .set at
  65. #endif
  66. /* Store a magic flag. */
  67. li v1, 1
  68. REG_S v1, (0 * SZREG + MCONTEXT_GREGS)(a0) /* zero */
  69. REG_S s0, (16 * SZREG + MCONTEXT_GREGS)(a0)
  70. REG_S s1, (17 * SZREG + MCONTEXT_GREGS)(a0)
  71. REG_S s2, (18 * SZREG + MCONTEXT_GREGS)(a0)
  72. REG_S s3, (19 * SZREG + MCONTEXT_GREGS)(a0)
  73. REG_S s4, (20 * SZREG + MCONTEXT_GREGS)(a0)
  74. REG_S s5, (21 * SZREG + MCONTEXT_GREGS)(a0)
  75. REG_S s6, (22 * SZREG + MCONTEXT_GREGS)(a0)
  76. REG_S s7, (23 * SZREG + MCONTEXT_GREGS)(a0)
  77. #if ! defined (__PIC__) || _MIPS_SIM != _ABIO32
  78. REG_S _GP, (28 * SZREG + MCONTEXT_GREGS)(a0)
  79. #endif
  80. REG_S _SP, (29 * SZREG + MCONTEXT_GREGS)(a0)
  81. REG_S fp, (30 * SZREG + MCONTEXT_GREGS)(a0)
  82. REG_S ra, (31 * SZREG + MCONTEXT_GREGS)(a0)
  83. REG_S ra, MCONTEXT_PC(a0)
  84. #ifdef __mips_hard_float
  85. # if _MIPS_SIM == _ABI64
  86. s.d fs0, (24 * SZREG + MCONTEXT_FPREGS)(a0)
  87. s.d fs1, (25 * SZREG + MCONTEXT_FPREGS)(a0)
  88. s.d fs2, (26 * SZREG + MCONTEXT_FPREGS)(a0)
  89. s.d fs3, (27 * SZREG + MCONTEXT_FPREGS)(a0)
  90. s.d fs4, (28 * SZREG + MCONTEXT_FPREGS)(a0)
  91. s.d fs5, (29 * SZREG + MCONTEXT_FPREGS)(a0)
  92. s.d fs6, (30 * SZREG + MCONTEXT_FPREGS)(a0)
  93. s.d fs7, (31 * SZREG + MCONTEXT_FPREGS)(a0)
  94. # else /* _MIPS_SIM != _ABI64 */
  95. s.d fs0, (20 * SZREG + MCONTEXT_FPREGS)(a0)
  96. s.d fs1, (22 * SZREG + MCONTEXT_FPREGS)(a0)
  97. s.d fs2, (24 * SZREG + MCONTEXT_FPREGS)(a0)
  98. s.d fs3, (26 * SZREG + MCONTEXT_FPREGS)(a0)
  99. s.d fs4, (28 * SZREG + MCONTEXT_FPREGS)(a0)
  100. s.d fs5, (30 * SZREG + MCONTEXT_FPREGS)(a0)
  101. # endif /* _MIPS_SIM != _ABI64 */
  102. cfc1 v1, fcr31
  103. sw v1, MCONTEXT_FPC_CSR(a0)
  104. #endif /* __mips_hard_float */
  105. REG_S a1, A1OFF(sp)
  106. /* rt_sigprocmask (SIG_SETMASK, &ucp->uc_sigmask, &oucp->uc_sigmask, _NSIG8) */
  107. li a3, _NSIG8
  108. PTR_ADDU a2, a0, UCONTEXT_SIGMASK
  109. PTR_ADDU a1, a1, UCONTEXT_SIGMASK
  110. li a0, SIG_SETMASK
  111. li v0, SYS_ify (rt_sigprocmask)
  112. syscall
  113. bnez a3, 99f
  114. REG_L v0, A1OFF(sp)
  115. #ifdef __mips_hard_float
  116. # if _MIPS_SIM == _ABI64
  117. l.d fs0, (24 * SZREG + MCONTEXT_FPREGS)(v0)
  118. l.d fs1, (25 * SZREG + MCONTEXT_FPREGS)(v0)
  119. l.d fs2, (26 * SZREG + MCONTEXT_FPREGS)(v0)
  120. l.d fs3, (27 * SZREG + MCONTEXT_FPREGS)(v0)
  121. l.d fs4, (28 * SZREG + MCONTEXT_FPREGS)(v0)
  122. l.d fs5, (29 * SZREG + MCONTEXT_FPREGS)(v0)
  123. l.d fs6, (30 * SZREG + MCONTEXT_FPREGS)(v0)
  124. l.d fs7, (31 * SZREG + MCONTEXT_FPREGS)(v0)
  125. # else /* _MIPS_SIM != _ABI64 */
  126. l.d fs0, (20 * SZREG + MCONTEXT_FPREGS)(v0)
  127. l.d fs1, (22 * SZREG + MCONTEXT_FPREGS)(v0)
  128. l.d fs2, (24 * SZREG + MCONTEXT_FPREGS)(v0)
  129. l.d fs3, (26 * SZREG + MCONTEXT_FPREGS)(v0)
  130. l.d fs4, (28 * SZREG + MCONTEXT_FPREGS)(v0)
  131. l.d fs5, (30 * SZREG + MCONTEXT_FPREGS)(v0)
  132. # endif /* _MIPS_SIM != _ABI64 */
  133. lw v1, MCONTEXT_FPC_CSR(v0)
  134. ctc1 v1, fcr31
  135. #endif /* __mips_hard_float */
  136. /* Note the contents of argument registers will be random
  137. unless makecontext() has been called. */
  138. REG_L a0, (4 * SZREG + MCONTEXT_GREGS)(v0)
  139. REG_L a1, (5 * SZREG + MCONTEXT_GREGS)(v0)
  140. REG_L a2, (6 * SZREG + MCONTEXT_GREGS)(v0)
  141. REG_L a3, (7 * SZREG + MCONTEXT_GREGS)(v0)
  142. #if _MIPS_SIM != _ABIO32
  143. REG_L a4, (8 * SZREG + MCONTEXT_GREGS)(v0)
  144. REG_L a5, (9 * SZREG + MCONTEXT_GREGS)(v0)
  145. REG_L a6, (10 * SZREG + MCONTEXT_GREGS)(v0)
  146. REG_L a7, (11 * SZREG + MCONTEXT_GREGS)(v0)
  147. #endif
  148. REG_L s0, (16 * SZREG + MCONTEXT_GREGS)(v0)
  149. REG_L s1, (17 * SZREG + MCONTEXT_GREGS)(v0)
  150. REG_L s2, (18 * SZREG + MCONTEXT_GREGS)(v0)
  151. REG_L s3, (19 * SZREG + MCONTEXT_GREGS)(v0)
  152. REG_L s4, (20 * SZREG + MCONTEXT_GREGS)(v0)
  153. REG_L s5, (21 * SZREG + MCONTEXT_GREGS)(v0)
  154. REG_L s6, (22 * SZREG + MCONTEXT_GREGS)(v0)
  155. REG_L s7, (23 * SZREG + MCONTEXT_GREGS)(v0)
  156. #if ! defined (__PIC__) || _MIPS_SIM != _ABIO32
  157. REG_L gp, (28 * SZREG + MCONTEXT_GREGS)(v0)
  158. #endif
  159. REG_L sp, (29 * SZREG + MCONTEXT_GREGS)(v0)
  160. REG_L fp, (30 * SZREG + MCONTEXT_GREGS)(v0)
  161. REG_L ra, (31 * SZREG + MCONTEXT_GREGS)(v0)
  162. REG_L t9, MCONTEXT_PC(v0)
  163. move v0, zero
  164. jr t9
  165. 99:
  166. #ifdef __PIC__
  167. PTR_LA t9, JUMPTARGET (__syscall_error)
  168. RESTORE_GP64
  169. PTR_ADDIU sp, FRAMESZ
  170. jr t9
  171. #else /* ! __PIC__ */
  172. j JUMPTARGET (__syscall_error)
  173. #endif /* ! __PIC__ */
  174. PSEUDO_END (__swapcontext)
  175. weak_alias (__swapcontext, swapcontext)