clone.S 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Pat Beirne <patb@corelcomputer.com>
  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. /* clone() is even more special than fork() as it mucks with stacks
  17. and invokes a function in the right context after its all over. */
  18. #define _ERRNO_H
  19. #include <bits/errno.h>
  20. #include <sys/syscall.h>
  21. #ifdef __NR_clone
  22. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg); */
  23. .text
  24. .global __clone
  25. .type __clone,%function
  26. .align 4
  27. __clone:
  28. @ sanity check args
  29. cmp r0, #0
  30. cmpne r1, #0
  31. moveq r0, #-EINVAL
  32. beq __error
  33. @ insert the args onto the new stack
  34. sub r1, r1, #8
  35. str r3, [r1, #4]
  36. @ save the function pointer as the 0th element
  37. str r0, [r1]
  38. @ do the system call
  39. @ get flags
  40. mov r0, r2
  41. @ new sp is already in r1
  42. swi __NR_clone
  43. movs a1, a1
  44. blt __error
  45. movne pc, lr
  46. @ pick the function arg and call address off the stack and execute
  47. ldr r0, [sp, #4]
  48. mov lr, pc
  49. ldr pc, [sp]
  50. @ and we are done, passing the return value through r0
  51. #ifdef __PIC__
  52. b _exit(PLT)
  53. #else
  54. b _exit
  55. #endif
  56. __error:
  57. b __syscall_error
  58. .size __clone,.-__clone
  59. .weak clone
  60. clone = __clone
  61. #endif