clone.S 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include "ppc_asm.h"
  17. #define _ERRNO_H 1
  18. #include <bits/errno.h>
  19. #include <sys/syscall.h>
  20. /* This is the only really unusual system call in PPC linux, but not
  21. because of any weirdness in the system call itself; because of
  22. all the freaky stuff we have to do to make the call useful. */
  23. /* int [r3] clone(int (*fn)(void *arg) [r3], void *child_stack [r4],
  24. int flags [r5], void *arg [r6]); */
  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. stmw r29,16(r1)
  38. /* Set up stack frame for child. */
  39. clrrwi r4,r4,4
  40. li r0,0
  41. stwu r0,-16(r4)
  42. /* Save fn, args, stack across syscall. */
  43. mr r29,r3 /* Function in r29. */
  44. mr r30,r4 /* Stack pointer in r30. */
  45. mr r31,r6 /* Argument in r31. */
  46. /* 'flags' argument is first parameter to clone syscall. (The other
  47. argument is the stack pointer, already in r4.) */
  48. mr r3,r5
  49. /* Do the call. */
  50. li 0, __NR_clone
  51. sc
  52. /* Check for child process. */
  53. cmpwi cr1,r3,0
  54. crandc cr1*4+eq,cr1*4+eq,cr0*4+so
  55. bne- cr1,.Lparent /* The '-' is to minimise the race. */
  56. /* On at least mklinux DR3a5, clone() doesn't actually change
  57. the stack pointer. I'm pretty sure this is a bug, because
  58. it adds a race condition if a signal is sent to a thread
  59. just after it is created (in the previous three instructions). */
  60. mr r1,r30
  61. /* Call procedure. */
  62. mtctr r29
  63. mr r3,r31
  64. bctrl
  65. /* Call _exit with result from procedure. */
  66. b _exit
  67. .Lparent:
  68. /* Parent. Restore registers & return. */
  69. lmw r29,16(r1)
  70. addi r1,r1,32
  71. bnslr+
  72. b __syscall_error
  73. .Lbadargs:
  74. li r3,EINVAL
  75. b __syscall_error
  76. .size __clone,.-__clone
  77. .weak clone;
  78. clone=__clone
  79. #endif