vfork.S 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. .literal .Ljumptable, 0, .L4, .L8, .L12
  43. mov a3, a0 # move return address out of the way
  44. movi a0, .Ljumptable
  45. extui a2, a3, 30, 2 # call-size: call4/8/12 = 1/2/3
  46. addx4 a0, a2, a0 # find return address in jumptable
  47. slli a2, a2, 30
  48. l32i a0, a0, 0
  49. xor a3, a3, a2 # remove call-size from return address
  50. or a0, a0, a2 # create temporary return address
  51. retw
  52. /* a7: return address */
  53. .L4: mov a12, a2
  54. mov a13, a3
  55. SAVE_PID(a5,a15,a2,a3)
  56. /* use syscall 'clone' and set new stack pointer to the same address */
  57. movi a2, SYS_ify(clone)
  58. movi a3, 0
  59. movi a6, CLONE_VM | CLONE_VFORK | SIGCHLD
  60. syscall
  61. RESTORE_PID(a5,a15,a2)
  62. movi a5, -4096
  63. mov a6, a2
  64. mov a2, a12
  65. mov a3, a13
  66. bgeu a6, a5, 1f
  67. jx a7
  68. 1: call4 .Lerr
  69. /* a11: return address */
  70. .L8: mov a12, a2
  71. mov a13, a3
  72. mov a14, a6
  73. SAVE_PID(a9,a15,a2,a3)
  74. movi a2, SYS_ify(clone)
  75. movi a3, 0
  76. movi a6, CLONE_VM | CLONE_VFORK | SIGCHLD
  77. syscall
  78. RESTORE_PID(a9,a15,a2)
  79. movi a9, -4096
  80. mov a10, a2
  81. mov a2, a12
  82. mov a3, a13
  83. mov a6, a14
  84. bgeu a10, a9, 1f
  85. jx a11
  86. 1: call8 .Lerr
  87. /* a15: return address */
  88. .L12: mov a12, a2
  89. mov a13, a3
  90. mov a14, a6
  91. SAVE_PID (a2,a3,a2,a6)
  92. movi a2, SYS_ify(clone)
  93. movi a3, 0
  94. movi a6, CLONE_VM | CLONE_VFORK | SIGCHLD
  95. syscall
  96. RESTORE_PID12(a3,a6,a15)
  97. mov a3, a13
  98. movi a13, -4096
  99. mov a6, a14
  100. mov a14, a2
  101. mov a2, a12
  102. bgeu a14, a13, 1f
  103. jx a15
  104. 1: call12 .Lerr
  105. .align 4
  106. .Lerr: entry a1, 16
  107. /* Restore return address */
  108. extui a4, a0, 30, 2
  109. slli a4, a4, 30
  110. or a0, a3, a4
  111. PSEUDO_END (__vfork)
  112. .Lpseudo_end:
  113. retw
  114. weak_alias (__vfork, vfork)
  115. libc_hidden_def(vfork)