clone.S 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  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, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. /* clone() is even more special than fork() as it mucks with stacks
  16. and invokes a function in the right context after its all over. */
  17. #define _ERRNO_H 1
  18. #include <bits/errno.h>
  19. #define CLONE_VM 0x00000100
  20. #define CLONE_THREAD 0x00010000
  21. /* The userland implementation is:
  22. int clone (int (*fn)(void *arg), void *child_stack, int flags, void *arg),
  23. the kernel entry is:
  24. int clone (long flags, void *child_stack).
  25. The parameters are passed in register and on the stack from userland:
  26. rdi: fn
  27. rsi: child_stack
  28. rdx: flags
  29. rcx: arg
  30. r8d: TID field in parent
  31. r9d: thread pointer
  32. %esp+8: TID field in child
  33. The kernel expects:
  34. rax: system call number
  35. rdi: flags
  36. rsi: child_stack
  37. rdx: TID field in parent
  38. r10: TID field in child
  39. r8: thread pointer */
  40. .text
  41. .globl __clone;
  42. .type __clone,@function
  43. .align 4;
  44. __clone:
  45. /* Sanity check arguments. */
  46. movq $-EINVAL,%rax
  47. testq %rdi,%rdi /* no NULL function pointers */
  48. jz __syscall_error
  49. testq %rsi,%rsi /* no NULL stack pointers */
  50. jz __syscall_error
  51. /* Insert the argument onto the new stack. */
  52. subq $16,%rsi
  53. movq %rcx,8(%rsi)
  54. /* Save the function pointer. It will be popped off in the
  55. child in the ebx frobbing below. */
  56. movq %rdi,0(%rsi)
  57. /* Do the system call. */
  58. movq %rdx, %rdi
  59. movq %r8, %rdx
  60. movq %r9, %r8
  61. movq 8(%rsp), %r10
  62. movq $__NR_clone,%rax
  63. syscall
  64. testq %rax,%rax
  65. jl __syscall_error
  66. jz L(thread_start)
  67. L(pseudo_end):
  68. ret
  69. L(thread_start):
  70. /* Clear the frame pointer. The ABI suggests this be done, to mark
  71. the outermost frame obviously. */
  72. xorq %rbp, %rbp
  73. #ifdef RESET_PID
  74. testq $CLONE_THREAD, %rdi
  75. jne 1f
  76. testq $CLONE_VM, %rdi
  77. movl $-1, %eax
  78. jne 2f
  79. movq __NR_getpid, %rax
  80. syscall
  81. 2: movl %eax, %fs:PID
  82. movl %eax, %fs:TID
  83. 1:
  84. #endif
  85. /* Set up arguments for the function call. */
  86. popq %rax /* Function to call. */
  87. popq %rdi /* Argument. */
  88. call *%rax
  89. /* Call exit with return value from function call. */
  90. movq %rax, %rdi
  91. call HIDDEN_JUMPTARGET (_exit)
  92. cfi_startproc;
  93. PSEUDO_END (BP_SYM (__clone))
  94. weak_alias (BP_SYM (__clone), BP_SYM (clone))