clone.S 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Copyright (C) 1996-2000,02,03,04,2005 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson (rth@tamu.edu)
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. /* clone() is even more special than fork() as it mucks with stacks
  15. and invokes a function in the right context after its all over.
  16. Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
  17. */
  18. #define _ERRNO_H 1
  19. #include <bits/errno.h>
  20. #include <sys/syscall.h>
  21. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
  22. pid_t *ptid, struct user_desc *tls, pid_t *ctid); */
  23. #define LINKAGE 4
  24. #define PTR_SIZE 4
  25. #define PARMS LINKAGE /* no space for saved regs */
  26. #define FUNC PARMS
  27. #define STACK FUNC+4
  28. #define FLAGS STACK+PTR_SIZE
  29. #define ARG FLAGS+4
  30. #define PTID ARG+PTR_SIZE
  31. #define TLS PTID+PTR_SIZE
  32. #define CTID TLS+PTR_SIZE
  33. .text
  34. .global clone
  35. .type clone,%function
  36. clone:
  37. /* Sanity check arguments. */
  38. movl $-EINVAL,%eax
  39. /* no NULL function pointers */
  40. movl FUNC(%esp),%ecx
  41. #ifdef __PIC__
  42. jecxz __error
  43. #else
  44. testl %ecx,%ecx
  45. jz __error
  46. #endif
  47. /* no NULL stack pointers */
  48. movl STACK(%esp),%ecx
  49. #ifdef __PIC__
  50. jecxz __error
  51. #else
  52. testl %ecx,%ecx
  53. jz __error
  54. #endif
  55. /* Insert the argument onto the new stack. Make sure the new
  56. thread is started with an alignment of (mod 16). */
  57. andl $0xfffffff0, %ecx
  58. subl $28,%ecx
  59. movl ARG(%esp),%eax /* no negative argument counts */
  60. movl %eax,12(%ecx)
  61. /* Save the function pointer as the zeroth argument.
  62. It will be popped off in the child in the ebx frobbing below. */
  63. movl FUNC(%esp),%eax
  64. movl %eax,8(%ecx)
  65. /* Don't leak any information. */
  66. movl $0,4(%ecx)
  67. /* Do the system call */
  68. pushl %ebx
  69. pushl %esi
  70. pushl %edi
  71. movl TLS+12(%esp),%esi
  72. movl PTID+12(%esp),%edx
  73. movl FLAGS+12(%esp),%ebx
  74. movl CTID+12(%esp),%edi
  75. movl $__NR_clone,%eax
  76. int $0x80
  77. popl %edi
  78. popl %esi
  79. popl %ebx
  80. test %eax,%eax
  81. jl __error
  82. jz .Lthread_start
  83. ret
  84. .Lthread_start:
  85. /* Note: %esi is zero. */
  86. movl %esi,%ebp /* terminate the stack frame */
  87. call *%ebx
  88. #ifdef __PIC__
  89. call .Lhere
  90. .Lhere:
  91. popl %ebx
  92. addl $_GLOBAL_OFFSET_TABLE_+[.-.Lhere], %ebx
  93. #endif
  94. movl %eax, %ebx
  95. movl $__NR_exit, %eax
  96. int $0x80
  97. /* Need to indirect jump to syscall error
  98. * or we end up with TEXTREL's
  99. */
  100. __error:
  101. jmp __syscall_error
  102. .size clone,.-clone
  103. weak_alias(clone, __clone)