clone.S 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Richard Henderson (rth@tamu.edu)
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. 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. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If not,
  14. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 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 <bits/errno.h>
  19. #include <sys/syscall.h>
  20. .text
  21. .align 4
  22. .type __clone,@function
  23. .globl __clone;
  24. __clone:
  25. /* Sanity check arguments. */
  26. movl 4(%esp),%ecx /* no NULL function pointers */
  27. jecxz CLONE_ERROR_LABEL
  28. movl 8(%esp),%ecx /* no NULL stack pointers */
  29. jecxz CLONE_ERROR_LABEL
  30. /* Insert the argument onto the new stack. */
  31. subl $8,%ecx
  32. movl 16(%esp),%eax /* no negative argument counts */
  33. movl %eax,4(%ecx)
  34. /* Save the function pointer as the zeroth argument.
  35. It will be popped off in the child in the ebx frobbing below. */
  36. movl 4(%esp),%eax
  37. movl %eax,0(%ecx)
  38. /* Do the system call */
  39. pushl %ebx
  40. movl 16(%esp),%ebx
  41. movl $__NR_clone,%eax
  42. int $0x80
  43. popl %ebx
  44. test %eax,%eax
  45. jl CLONE_ERROR_LABEL
  46. jne CLONE_RETURN_LABEL
  47. /* Start thread */
  48. subl %ebp,%ebp /* terminate the stack frame */
  49. call *%ebx
  50. pushl %eax
  51. call _exit
  52. CLONE_ERROR_LABEL:
  53. negl %eax
  54. pushl %eax
  55. #ifdef PIC
  56. call L(here)
  57. L(here):
  58. popl %ebx
  59. addl $_GLOBAL_OFFSET_TABLE_+[.-L(here)], %ebx
  60. call __errno_location@PLT
  61. #else
  62. call __errno_location
  63. #endif
  64. popl %ecx
  65. movl %ecx, (%eax)
  66. xorl %eax, %eax
  67. decl %eax
  68. CLONE_RETURN_LABEL:
  69. ret
  70. .globl clone;
  71. clone = __clone