clone.S 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 <asm/errno.h>
  19. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg); */
  20. .text
  21. .globl __clone;
  22. .align 4; \
  23. __clone:
  24. /* Sanity check arguments. */
  25. movl $-EINVAL,%eax
  26. movl 4(%esp),%ecx /* no NULL function pointers */
  27. #ifdef PIC
  28. jecxz SYSCALL_ERROR_LABEL
  29. #else
  30. testl %ecx,%ecx
  31. jz SYSCALL_ERROR_LABEL
  32. #endif
  33. movl 8(%esp),%ecx /* no NULL stack pointers */
  34. #ifdef PIC
  35. jecxz SYSCALL_ERROR_LABEL
  36. #else
  37. testl %ecx,%ecx
  38. jz SYSCALL_ERROR_LABEL
  39. #endif
  40. /* Insert the argument onto the new stack. */
  41. subl $8,%ecx
  42. movl 16(%esp),%eax /* no negative argument counts */
  43. movl %eax,4(%ecx)
  44. /* Save the function pointer as the zeroth argument.
  45. It will be popped off in the child in the ebx frobbing below. */
  46. movl 4(%esp),%eax
  47. movl %eax,0(%ecx)
  48. /* Do the system call */
  49. pushl %ebx
  50. movl 16(%esp),%ebx
  51. movl $__NR_clone,%eax
  52. int $0x80
  53. popl %ebx
  54. test %eax,%eax
  55. jl SYSCALL_ERROR_LABEL
  56. jz thread_start
  57. L(pseudo_end):
  58. ret
  59. thread_start:
  60. subl %ebp,%ebp /* terminate the stack frame */
  61. call *%ebx
  62. #ifdef PIC
  63. call L(here)
  64. L(here):
  65. popl %ebx
  66. addl $_GLOBAL_OFFSET_TABLE_+[.-L(here)], %ebx
  67. #endif
  68. pushl %eax
  69. call _exit