clone.S 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Wrapper around clone system call.
  2. Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <features.h>
  16. #define _ERRNO_H 1
  17. #include <bits/errno.h>
  18. #include <sysdep.h>
  19. #define CLONE_VM 0x00000100
  20. #define CLONE_THREAD 0x00010000
  21. /* This is the only really unusual system call in PPC linux, but not
  22. because of any weirdness in the system call itself; because of
  23. all the freaky stuff we have to do to make the call useful. */
  24. /* int [r3] clone(int (*fn)(void *arg) [r3], void *child_stack [r4],
  25. int flags [r5], void *arg [r6], void *parent_tid [r7],
  26. void *tls [r8], void *child_tid [r9]); */
  27. #ifdef __NR_clone
  28. .globl __clone
  29. .type __clone,@function
  30. .align 2
  31. __clone:
  32. /* Check for child_stack == NULL || fn == NULL. */
  33. cmpwi cr0,r4,0
  34. cmpwi cr1,r3,0
  35. cror cr0*4+eq,cr1*4+eq,cr0*4+eq
  36. beq- cr0,.Lbadargs
  37. /* Set up stack frame for parent. */
  38. stwu r1,-32(r1)
  39. cfi_adjust_cfa_offset (32)
  40. #ifdef RESET_PID
  41. stmw r28,16(r1)
  42. #else
  43. # ifndef __ASSUME_FIXED_CLONE_SYSCALL
  44. stmw r29,16(r1)
  45. # else
  46. stmw r30,16(r1)
  47. # endif
  48. #endif
  49. /* Set up stack frame for child. */
  50. clrrwi r4,r4,4
  51. li r0,0
  52. stwu r0,-16(r4)
  53. /* Save fn, args, stack across syscall. */
  54. mr r30,r3 /* Function in r30. */
  55. #ifndef __ASSUME_FIXED_CLONE_SYSCALL
  56. mr r29,r4 /* Stack pointer in r29. */
  57. #endif
  58. #ifdef RESET_PID
  59. mr r28,r5
  60. #endif
  61. mr r31,r6 /* Argument in r31. */
  62. /* 'flags' argument is first parameter to clone syscall. (The other
  63. argument is the stack pointer, already in r4.) */
  64. mr r3,r5
  65. /* Move the parent_tid, child_tid and tls arguments. */
  66. mr r5,r7
  67. mr r6,r8
  68. mr r7,r9
  69. /* End FDE now, because in the child the unwind info will be wrong. */
  70. cfi_endproc
  71. /* Do the call. */
  72. li 0, __NR_clone
  73. sc
  74. /* Check for child process. */
  75. cmpwi cr1,r3,0
  76. crandc cr1*4+eq,cr1*4+eq,cr0*4+so
  77. bne- cr1,.Lparent /* The '-' is to minimise the race. */
  78. #ifndef __ASSUME_FIXED_CLONE_SYSCALL
  79. /* On at least mklinux DR3a5, clone() doesn't actually change
  80. the stack pointer. I'm pretty sure this is a bug, because
  81. it adds a race condition if a signal is sent to a thread
  82. just after it is created (in the previous three instructions). */
  83. mr r1,r29
  84. #endif
  85. #ifdef RESET_PID
  86. andis. r0,r28,CLONE_THREAD>>16
  87. bne+ r0,.Loldpid
  88. andi. r0,r28,CLONE_VM
  89. li r3,-1
  90. bne- r0,.Lnomoregetpid
  91. .Lnomoregetpid:
  92. stw r3,TID(r2)
  93. stw r3,PID(r2)
  94. .Loldpid:
  95. #endif
  96. /* Call procedure. */
  97. mtctr r30
  98. mr r3,r31
  99. bctrl
  100. /* Call _exit with result from procedure. */
  101. b HIDDEN_JUMPTARGET(_exit)
  102. .Lparent:
  103. /* Parent. Restore registers & return. */
  104. #ifdef RESET_PID
  105. lmw r28,16(r1)
  106. #else
  107. # ifndef __ASSUME_FIXED_CLONE_SYSCALL
  108. lmw r29,16(r1)
  109. # else
  110. lmw r30,16(r1)
  111. # endif
  112. #endif
  113. addi r1,r1,32
  114. bnslr+
  115. b __syscall_error
  116. .Lbadargs:
  117. li r3,EINVAL
  118. b __syscall_error
  119. cfi_startproc
  120. .size __clone,.-__clone
  121. weak_alias(__clone, clone)
  122. #endif