vfork.S 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* Copyright (C) 2005-2013 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include "sysdep.h"
  15. #include <sys/syscall.h>
  16. #define _SIGNAL_H
  17. #include <bits/signum.h>
  18. #define __ASSEMBLY__
  19. #include <linux/sched.h>
  20. /*
  21. Clone the calling process, but without copying the whole address space.
  22. The calling process is suspended until the new process exits or is
  23. replaced by a call to `execve'. Return -1 for errors, 0 to the new process,
  24. and the process ID of the new process to the old process.
  25. Note that it is important that we don't create a new stack frame for the
  26. caller.
  27. */
  28. #ifndef SAVE_PID
  29. #define SAVE_PID(a,b,c,d)
  30. #endif
  31. #ifndef RESTORE_PID
  32. #define RESTORE_PID(a,b,c)
  33. #endif
  34. #ifndef RESTORE_PID12
  35. #define RESTORE_PID12(a,b,c)
  36. #endif
  37. /*
  38. pid_t vfork(void);
  39. Implemented as __clone_syscall(CLONE_VFORK | CLONE_VM | SIGCHLD, 0)
  40. */
  41. HIDDEN_ENTRY (__vfork)
  42. #if defined(__XTENSA_WINDOWED_ABI__)
  43. .literal .Ljumptable, 0, .L4, .L8, .L12
  44. mov a3, a0 # move return address out of the way
  45. movi a0, .Ljumptable
  46. extui a2, a3, 30, 2 # call-size: call4/8/12 = 1/2/3
  47. addx4 a0, a2, a0 # find return address in jumptable
  48. l32i a0, a0, 0
  49. # exchange top 2 bits of a0 and a3:
  50. xor a2, a0, a3
  51. extui a2, a2, 30, 2
  52. slli a2, a2, 30
  53. xor a0, a0, a2
  54. xor a3, a3, a2
  55. retw
  56. /* a7: return address */
  57. .L4: mov a12, a2
  58. mov a13, a3
  59. SAVE_PID(a5,a15,a2,a3)
  60. /* use syscall 'clone' and set new stack pointer to the same address */
  61. movi a2, SYS_ify(clone)
  62. movi a3, 0
  63. movi a6, CLONE_VM | CLONE_VFORK | SIGCHLD
  64. syscall
  65. RESTORE_PID(a5,a15,a2)
  66. movi a5, -4096
  67. mov a6, a2
  68. mov a2, a12
  69. mov a3, a13
  70. bgeu a6, a5, 1f
  71. jx a7
  72. 1: call4 .Lerr
  73. /* a11: return address */
  74. .L8: mov a12, a2
  75. mov a13, a3
  76. mov a14, a6
  77. SAVE_PID(a9,a15,a2,a3)
  78. movi a2, SYS_ify(clone)
  79. movi a3, 0
  80. movi a6, CLONE_VM | CLONE_VFORK | SIGCHLD
  81. syscall
  82. RESTORE_PID(a9,a15,a2)
  83. movi a9, -4096
  84. mov a10, a2
  85. mov a2, a12
  86. mov a3, a13
  87. mov a6, a14
  88. bgeu a10, a9, 1f
  89. jx a11
  90. 1: call8 .Lerr
  91. /* a15: return address */
  92. .L12: mov a12, a2
  93. mov a13, a3
  94. mov a14, a6
  95. SAVE_PID (a2,a3,a2,a6)
  96. movi a2, SYS_ify(clone)
  97. movi a3, 0
  98. movi a6, CLONE_VM | CLONE_VFORK | SIGCHLD
  99. syscall
  100. RESTORE_PID12(a3,a6,a15)
  101. mov a3, a13
  102. movi a13, -4096
  103. mov a6, a14
  104. mov a14, a2
  105. mov a2, a12
  106. bgeu a14, a13, 1f
  107. jx a15
  108. 1: call12 .Lerr
  109. .align 4
  110. .Lerr: entry a1, 16
  111. /* Restore return address */
  112. extui a4, a0, 30, 2
  113. slli a4, a4, 30
  114. or a0, a3, a4
  115. PSEUDO_END (__vfork)
  116. .Lpseudo_end:
  117. retw
  118. #elif defined(__XTENSA_CALL0_ABI__)
  119. SAVE_PID(a5, a8, a3, a4)
  120. /* Use syscall 'clone'. Set new stack pointer to the same address. */
  121. movi a2, SYS_ify (clone)
  122. movi a3, 0
  123. movi a6, CLONE_VM | CLONE_VFORK | SIGCHLD
  124. syscall
  125. RESTORE_PID(a5, a8, a2)
  126. movi a3, -4096
  127. bgeu a2, a3, 1f
  128. ret
  129. 1:
  130. PSEUDO_END (__vfork)
  131. .Lpseudo_end:
  132. ret
  133. #else
  134. #error Unsupported Xtensa ABI
  135. #endif
  136. weak_alias (__vfork, vfork)
  137. libc_hidden_def(vfork)