clone.S 2.3 KB

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