clone.S 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /* This is the only really unusual system call in PPC linux, but not
  20. because of any weirdness in the system call itself; because of
  21. all the freaky stuff we have to do to make the call useful. */
  22. /* int [r3] clone(int (*fn)(void *arg) [r3], void *child_stack [r4],
  23. int flags [r5], void *arg [r6], void *parent_tid [r7],
  24. void *tls [r8], void *child_tid [r9]); */
  25. #ifdef __NR_clone
  26. .globl __clone
  27. .type __clone,@function
  28. .align 2
  29. __clone:
  30. /* Check for child_stack == NULL || fn == NULL. */
  31. cmpwi cr0,r4,0
  32. cmpwi cr1,r3,0
  33. cror cr0*4+eq,cr1*4+eq,cr0*4+eq
  34. beq- cr0,.Lbadargs
  35. /* Set up stack frame for parent. */
  36. stwu r1,-32(r1)
  37. cfi_adjust_cfa_offset (32)
  38. #ifndef __ASSUME_FIXED_CLONE_SYSCALL
  39. stmw r29,16(r1)
  40. #else
  41. stmw r30,16(r1)
  42. #endif
  43. /* Set up stack frame for child. */
  44. clrrwi r4,r4,4
  45. li r0,0
  46. stwu r0,-16(r4)
  47. /* Save fn, args, stack across syscall. */
  48. mr r30,r3 /* Function in r30. */
  49. #ifndef __ASSUME_FIXED_CLONE_SYSCALL
  50. mr r29,r4 /* Stack pointer in r29. */
  51. #endif
  52. mr r31,r6 /* Argument in r31. */
  53. /* 'flags' argument is first parameter to clone syscall. (The other
  54. argument is the stack pointer, already in r4.) */
  55. mr r3,r5
  56. /* Move the parent_tid, child_tid and tls arguments. */
  57. mr r5,r7
  58. mr r6,r8
  59. mr r7,r9
  60. /* End FDE now, because in the child the unwind info will be wrong. */
  61. cfi_endproc
  62. /* Do the call. */
  63. li 0, __NR_clone
  64. sc
  65. /* Check for child process. */
  66. cmpwi cr1,r3,0
  67. crandc cr1*4+eq,cr1*4+eq,cr0*4+so
  68. bne- cr1,.Lparent /* The '-' is to minimise the race. */
  69. #ifndef __ASSUME_FIXED_CLONE_SYSCALL
  70. /* On at least mklinux DR3a5, clone() doesn't actually change
  71. the stack pointer. I'm pretty sure this is a bug, because
  72. it adds a race condition if a signal is sent to a thread
  73. just after it is created (in the previous three instructions). */
  74. mr r1,r29
  75. #endif
  76. /* Call procedure. */
  77. mtctr r30
  78. mr r3,r31
  79. bctrl
  80. /* Call _exit with result from procedure. */
  81. b HIDDEN_JUMPTARGET(_exit)
  82. .Lparent:
  83. /* Parent. Restore registers & return. */
  84. #ifndef __ASSUME_FIXED_CLONE_SYSCALL
  85. lmw r29,16(r1)
  86. #else
  87. lmw r30,16(r1)
  88. #endif
  89. addi r1,r1,32
  90. bnslr+
  91. b __syscall_error
  92. .Lbadargs:
  93. li r3,EINVAL
  94. b __syscall_error
  95. cfi_startproc
  96. .size __clone,.-__clone
  97. weak_alias(__clone, clone)
  98. #endif