clone.S 2.2 KB

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