clone.S 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include <asm/errno.h>
  19. #include <sys/syscall.h>
  20. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg); */
  21. .text
  22. .globl __clone;
  23. .type __clone,%function
  24. .align 4;
  25. __clone:
  26. @ sanity check args
  27. cmp r0, #0
  28. cmpne r1, #0
  29. moveq r0, #-EINVAL
  30. beq __syscall_error (PLT)
  31. @ insert the args onto the new stack
  32. sub r1, r1, #8
  33. str r3, [r1, #4]
  34. @ save the function pointer as the 0th element
  35. str r0, [r1]
  36. @ do the system call
  37. @ get flags
  38. mov r0, r2
  39. @ new sp is already in r1
  40. swi __NR_clone
  41. movs a1, a1
  42. blt __syscall_error (PLT)
  43. movne pc, lr
  44. @ pick the function arg and call address off the stack and execute
  45. ldr r0, [sp, #4]
  46. mov lr, pc
  47. ldr pc, [sp]
  48. @ and we are done, passing the return value through r0
  49. b _exit (PLT)
  50. __syscall_error:
  51. /* Looks like the syscall choked -- set errno */
  52. ldr r3, .L4
  53. /* Calculate the - of the syscall result, in case we need it */
  54. rsb r2, r0, $0
  55. /* errno = -result */
  56. str r2, [r9,r3]
  57. /* return -1 */
  58. mvn r0, $0
  59. mov pc, lr
  60. .size __clone,.-__clone;
  61. .L4: .word errno
  62. .globl clone;
  63. clone = __clone