clone.S 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (C) 2001, 2002, 2003, 2004, 2005 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, 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. #include <features.h>
  17. #define _ERRNO_H 1
  18. #include <bits/errno.h>
  19. #include <sys/syscall.h>
  20. #define CLONE_VM 0x00000100
  21. #define CLONE_THREAD 0x00010000
  22. /* The userland implementation is:
  23. int clone (int (*fn)(void *arg), void *child_stack, int flags, void *arg),
  24. the kernel entry is:
  25. int clone (long flags, void *child_stack).
  26. The parameters are passed in register and on the stack from userland:
  27. rdi: fn
  28. rsi: child_stack
  29. rdx: flags
  30. rcx: arg
  31. r8d: TID field in parent
  32. r9d: thread pointer
  33. %esp+8: TID field in child
  34. The kernel expects:
  35. rax: system call number
  36. rdi: flags
  37. rsi: child_stack
  38. rdx: TID field in parent
  39. r10: TID field in child
  40. r8: thread pointer */
  41. .text
  42. .global clone
  43. .type clone,%function
  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. movl $__NR_clone,%eax
  63. syscall
  64. testq %rax,%rax
  65. jl __syscall_error
  66. jz .Lthread_start
  67. .Lpseudo_end:
  68. ret
  69. .Lthread_start:
  70. /* Clear the frame pointer. The ABI suggests this be done, to mark
  71. the outermost frame obviously. */
  72. xorl %ebp, %ebp
  73. /* Set up arguments for the function call. */
  74. popq %rax /* Function to call. */
  75. popq %rdi /* Argument. */
  76. call *%rax
  77. /* Call exit with return value from function call. */
  78. movq %rax, %rdi
  79. movl $__NR_exit, %eax
  80. syscall
  81. .size clone,.-clone
  82. weak_alias(clone, __clone)