vfork.S 3.8 KB

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