makecontext.S 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* Modify saved 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 makecontext (ucontext_t *ucp, (void *func) (), int argc, ...) */
  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 = 5 /* save a3-a7 */
  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. A3OFF = FRAMESZ - (5 * SZREG) /* callee-allocated */
  39. A4OFF = FRAMESZ - (4 * SZREG)
  40. A5OFF = FRAMESZ - (3 * SZREG)
  41. A6OFF = FRAMESZ - (2 * SZREG)
  42. A7OFF = FRAMESZ - (1 * SZREG)
  43. NARGREGS = 8
  44. #else
  45. A3OFF = FRAMESZ + (3 * SZREG) /* caller-allocated */
  46. NARGREGS = 4
  47. #endif
  48. NESTED (__makecontext, FRAMESZ, ra)
  49. .mask MASK, -(ARGSZ * SZREG)
  50. .fmask 0x00000000, 0
  51. 98:
  52. #ifdef __PIC__
  53. SETUP_GP
  54. #endif
  55. PTR_ADDIU sp, -FRAMESZ
  56. #ifdef __PIC__
  57. SETUP_GP64 (GPOFF, __makecontext)
  58. SAVE_GP (GPOFF)
  59. #endif
  60. #ifdef PROF
  61. .set noat
  62. move AT, ra
  63. jal _mcount
  64. .set at
  65. #endif
  66. /* Store args to be passed. */
  67. REG_S a3, A3OFF(sp)
  68. #if _MIPS_SIM != _ABIO32
  69. REG_S a4, A4OFF(sp)
  70. REG_S a5, A5OFF(sp)
  71. REG_S a6, A6OFF(sp)
  72. REG_S a7, A7OFF(sp)
  73. #endif
  74. /* Store a magic flag. */
  75. li v1, 1
  76. REG_S v1, (0 * SZREG + MCONTEXT_GREGS)(a0) /* zero */
  77. /* Set up the stack. */
  78. PTR_L t0, STACK_SP(a0)
  79. PTR_L t2, STACK_SIZE(a0)
  80. PTR_ADDIU t1, sp, A3OFF
  81. PTR_ADDU t0, t2
  82. and t0, ALMASK
  83. blez a2, 2f /* no arguments */
  84. /* Store register arguments. */
  85. PTR_ADDIU t2, a0, MCONTEXT_GREGS + 4 * SZREG
  86. move t3, zero
  87. 0:
  88. addiu t3, 1
  89. REG_L v1, (t1)
  90. PTR_ADDIU t1, SZREG
  91. REG_S v1, (t2)
  92. PTR_ADDIU t2, SZREG
  93. bgeu t3, a2, 2f /* all done */
  94. bltu t3, NARGREGS, 0b /* next */
  95. /* Make room for stack arguments. */
  96. PTR_SUBU t2, a2, t3
  97. PTR_SLL t2, 3
  98. PTR_SUBU t0, t2
  99. and t0, ALMASK
  100. /* Store stack arguments. */
  101. move t2, t0
  102. 1:
  103. addiu t3, 1
  104. REG_L v1, (t1)
  105. PTR_ADDIU t1, SZREG
  106. REG_S v1, (t2)
  107. PTR_ADDIU t2, SZREG
  108. bltu t3, a2, 1b /* next */
  109. 2:
  110. #if _MIPS_SIM == _ABIO32
  111. /* Make room for a0-a3 storage. */
  112. PTR_ADDIU t0, -(NARGSAVE * SZREG)
  113. #endif
  114. PTR_L v1, UCONTEXT_LINK(a0)
  115. #ifdef __PIC__
  116. PTR_ADDIU t9, 99f - 98b
  117. #else
  118. PTR_LA t9, 99f
  119. #endif
  120. REG_S t0, (29 * SZREG + MCONTEXT_GREGS)(a0) /* sp */
  121. REG_S v1, (16 * SZREG + MCONTEXT_GREGS)(a0) /* s0 */
  122. #ifdef __PIC__
  123. REG_S gp, (17 * SZREG + MCONTEXT_GREGS)(a0) /* s1 */
  124. #endif
  125. REG_S t9, (31 * SZREG + MCONTEXT_GREGS)(a0) /* ra */
  126. REG_S a1, MCONTEXT_PC(a0)
  127. #ifdef __PIC__
  128. RESTORE_GP64
  129. PTR_ADDIU sp, FRAMESZ
  130. #endif
  131. jr ra
  132. 99:
  133. #ifdef __PIC__
  134. move gp, s1
  135. #endif
  136. move a0, zero
  137. beqz s0, 0f
  138. /* setcontext (ucp) */
  139. move a0, s0
  140. #ifdef __PIC__
  141. PTR_LA t9, JUMPTARGET (__setcontext)
  142. jalr t9
  143. # if _MIPS_SIM == _ABIO32
  144. move gp, s1
  145. # endif
  146. #else
  147. jal JUMPTARGET (__setcontext)
  148. #endif
  149. move a0, v0
  150. 0:
  151. /* exit (a0) */
  152. #ifdef __PIC__
  153. PTR_LA t9, HIDDEN_JUMPTARGET (exit)
  154. jalr t9
  155. #else
  156. jal HIDDEN_JUMPTARGET (exit)
  157. #endif
  158. /* You don't exist, you won't feel anything. */
  159. 1:
  160. lb zero, (zero)
  161. b 1b
  162. PSEUDO_END (__makecontext)
  163. weak_alias (__makecontext, makecontext)