vfork.S 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* Copyright (C) 2005, 2007 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. /* Clone the calling process, but without copying the whole address space.
  19. The calling process is suspended until the new process exits or is
  20. replaced by a call to `execve'. Return -1 for errors, 0 to the new process,
  21. and the process ID of the new process to the old process.
  22. Note that it is important that we don't create a new stack frame for the
  23. caller. */
  24. /* The following are defined in linux/sched.h, which unfortunately
  25. is not safe for inclusion in an assembly file. */
  26. #define CLONE_VM 0x00000100 /* set if VM shared between processes */
  27. #define CLONE_VFORK 0x00004000 /* set if the parent wants the child to
  28. wake it up on mm_release */
  29. #ifndef SAVE_PID
  30. #define SAVE_PID
  31. #endif
  32. #ifndef RESTORE_PID
  33. #define RESTORE_PID
  34. #endif
  35. /* pid_t vfork(void);
  36. Implemented as __clone_syscall(CLONE_VFORK | CLONE_VM | SIGCHLD, 0) */
  37. HIDDEN_ENTRY (__vfork)
  38. movi a6, .Ljumptable
  39. extui a2, a0, 30, 2 /* call-size: call4/8/12 = 1/2/3 */
  40. addx4 a4, a2, a6 /* find return address in jumptable */
  41. l32i a4, a4, 0
  42. add a4, a4, a6
  43. slli a2, a2, 30
  44. xor a3, a0, a2 /* remove call-size from return addr */
  45. extui a5, a4, 30, 2 /* get high bits of jump target */
  46. slli a5, a5, 30
  47. or a3, a3, a5 /* stuff them into the return address */
  48. xor a4, a4, a5 /* clear high bits of jump target */
  49. or a0, a4, a2 /* create temporary return address */
  50. retw /* "return" to .L4, .L8, or .L12 */
  51. .align 4
  52. .Ljumptable:
  53. .word 0
  54. .word .L4 - .Ljumptable
  55. .word .L8 - .Ljumptable
  56. .word .L12 - .Ljumptable
  57. /* a7: return address */
  58. .L4: mov a12, a2
  59. mov a13, a3
  60. SAVE_PID
  61. /* Use syscall 'clone'. Set new stack pointer to the same address. */
  62. movi a2, SYS_ify (clone)
  63. movi a3, 0
  64. movi a6, CLONE_VM | CLONE_VFORK | SIGCHLD
  65. syscall
  66. RESTORE_PID
  67. movi a5, -4096
  68. mov a6, a2
  69. mov a2, a12
  70. mov a3, a13
  71. bgeu a6, a5, 1f
  72. jx a7
  73. 1: call4 .Lerr /* returns to original caller */
  74. /* a11: return address */
  75. .L8: mov a12, a2
  76. mov a13, a3
  77. mov a14, a6
  78. SAVE_PID
  79. movi a2, SYS_ify (clone)
  80. movi a3, 0
  81. movi a6, CLONE_VM | CLONE_VFORK | SIGCHLD
  82. syscall
  83. RESTORE_PID
  84. movi a9, -4096
  85. mov a10, a2
  86. mov a2, a12
  87. mov a3, a13
  88. mov a6, a14
  89. bgeu a10, a9, 1f
  90. jx a11
  91. 1: call8 .Lerr /* returns to original caller */
  92. /* a15: return address */
  93. .L12: mov a12, a2
  94. mov a13, a3
  95. mov a14, a6
  96. SAVE_PID
  97. movi a2, SYS_ify (clone)
  98. movi a3, 0
  99. movi a6, CLONE_VM | CLONE_VFORK | SIGCHLD
  100. syscall
  101. RESTORE_PID
  102. mov a3, a13
  103. movi a13, -4096
  104. mov a6, a14
  105. mov a14, a2
  106. mov a2, a12
  107. bgeu a14, a13, 1f
  108. jx a15
  109. 1: call12 .Lerr /* returns to original caller */
  110. .align 4
  111. .Lerr: entry a1, 16
  112. /* Restore the return address. */
  113. extui a4, a0, 30, 2 /* get the call-size bits */
  114. slli a4, a4, 30
  115. slli a3, a3, 2 /* clear high bits of target address */
  116. srli a3, a3, 2
  117. or a0, a3, a4 /* combine them */
  118. PSEUDO_END (__vfork)
  119. .Lpseudo_end:
  120. retw
  121. weak_alias (__vfork, vfork)
  122. libc_hidden_def(vfork)