clone.S 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Copyright (C) 1996-2000,02,03,04,2005 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 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. Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
  19. */
  20. #define _ERRNO_H 1
  21. #include <bits/errno.h>
  22. #include <sys/syscall.h>
  23. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
  24. pid_t *ptid, struct user_desc *tls, pid_t *ctid); */
  25. #define LINKAGE 4
  26. #define PTR_SIZE 4
  27. #define PARMS LINKAGE /* no space for saved regs */
  28. #define FUNC PARMS
  29. #define STACK FUNC+4
  30. #define FLAGS STACK+PTR_SIZE
  31. #define ARG FLAGS+4
  32. #define PTID ARG+PTR_SIZE
  33. #define TLS PTID+PTR_SIZE
  34. #define CTID TLS+PTR_SIZE
  35. .text
  36. .global __clone
  37. .type __clone,%function
  38. __clone:
  39. /* Sanity check arguments. */
  40. movl $-EINVAL,%eax
  41. /* no NULL function pointers */
  42. movl FUNC(%esp),%ecx
  43. #ifdef __PIC__
  44. jecxz __error
  45. #else
  46. testl %ecx,%ecx
  47. jz __error
  48. #endif
  49. /* no NULL stack pointers */
  50. movl STACK(%esp),%ecx
  51. #ifdef __PIC__
  52. jecxz __error
  53. #else
  54. testl %ecx,%ecx
  55. jz __error
  56. #endif
  57. /* Insert the argument onto the new stack. Make sure the new
  58. thread is started with an alignment of (mod 16). */
  59. andl $0xfffffff0, %ecx
  60. subl $28,%ecx
  61. movl ARG(%esp),%eax /* no negative argument counts */
  62. movl %eax,12(%ecx)
  63. /* Save the function pointer as the zeroth argument.
  64. It will be popped off in the child in the ebx frobbing below. */
  65. movl FUNC(%esp),%eax
  66. movl %eax,8(%ecx)
  67. /* Don't leak any information. */
  68. movl $0,4(%ecx)
  69. movl $0,(%ecx)
  70. /* Do the system call */
  71. pushl %ebx
  72. pushl %esi
  73. pushl %edi
  74. movl TLS+12(%esp),%esi
  75. movl PTID+12(%esp),%edx
  76. movl FLAGS+12(%esp),%ebx
  77. movl CTID+12(%esp),%edi
  78. movl $__NR_clone,%eax
  79. int $0x80
  80. popl %edi
  81. popl %esi
  82. popl %ebx
  83. test %eax,%eax
  84. jl __error
  85. jz .Lthread_start
  86. ret
  87. .Lthread_start:
  88. /* Note: %esi is zero. */
  89. movl %esi,%ebp /* terminate the stack frame */
  90. call *%ebx
  91. #ifdef __PIC__
  92. call .Lhere
  93. .Lhere:
  94. popl %ebx
  95. addl $_GLOBAL_OFFSET_TABLE_+[.-.Lhere], %ebx
  96. #endif
  97. movl %eax, %ebx
  98. movl $__NR_exit, %eax
  99. int $0x80
  100. /* Need to indirect jump to syscall error
  101. * or we end up with TEXTREL's
  102. */
  103. __error:
  104. jmp __syscall_error
  105. .size __clone,.-__clone
  106. .weak clone
  107. clone = __clone