clone.S 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Copyright (C) 1996-2017 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 <sysdep.h>
  17. #define _ERRNO_H 1
  18. #include <bits/errno.h>
  19. #define CLONE_VM_BIT 8
  20. #define CLONE_VM (1 << CLONE_VM_BIT)
  21. #define CLONE_THREAD_BIT 16
  22. #define CLONE_THREAD (1 << CLONE_THREAD_BIT)
  23. /* int clone(int (*fn)(void *arg), x0
  24. void *child_stack, x1
  25. int flags, x2
  26. void *arg, x3
  27. pid_t *ptid, x4
  28. struct user_desc *tls, x5
  29. pid_t *ctid); x6
  30. */
  31. .text
  32. ENTRY(__clone)
  33. /* Save args for the child. */
  34. mov x10, x0
  35. mov x11, x2
  36. mov x12, x3
  37. /* Sanity check args. */
  38. mov x0, #-EINVAL
  39. cbz x10, .Lsyscall_error
  40. cbz x1, .Lsyscall_error
  41. /* Do the system call. */
  42. /* X0:flags, x1:newsp, x2:parenttidptr, x3:newtls, x4:childtid. */
  43. mov x0, x2 /* flags */
  44. /* New sp is already in x1. */
  45. mov x2, x4 /* ptid */
  46. mov x3, x5 /* tls */
  47. mov x4, x6 /* ctid */
  48. mov x8, #SYS_ify(clone)
  49. svc 0x0
  50. cmp x0, #0
  51. beq thread_start
  52. blt .Lsyscall_error
  53. RET
  54. PSEUDO_END (__clone)
  55. .align 4
  56. .type thread_start, %function
  57. thread_start:
  58. cfi_startproc
  59. cfi_undefined (x30)
  60. mov x29, 0
  61. /* Pick the function arg and execute. */
  62. mov x0, x12
  63. blr x10
  64. /* We are done, pass the return value through x0. */
  65. b HIDDEN_JUMPTARGET(_exit)
  66. cfi_endproc
  67. .size thread_start, .-thread_start
  68. libc_hidden_def (__clone)
  69. weak_alias (__clone, clone)