clone.S 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (C) 2026 Waldemar Brodkorb <wbx@uclibc-ng.org>
  3. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  4. */
  5. /* The clone syscall wrapper.
  6. Copyright (C) 2022-2026 Free Software Foundation, Inc.
  7. The GNU C Library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The GNU C Library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the GNU C Library. If not, see
  17. <https://www.gnu.org/licenses/>. */
  18. /* clone() is even more special than fork() as it mucks with stacks
  19. and invokes a function in the right context after its all over. */
  20. #include <sys/asm.h>
  21. #include <sys/syscall.h>
  22. #define _ERRNO_H 1
  23. #include <bits/errno.h>
  24. #define L(label) .L##label
  25. /* Align reg to 2^n. Used in assembly. */
  26. #define REG_ALIGN_ASM(reg, n) bstrins.d reg, zero, (n-1), 0
  27. #define STACK_ALIGN 16
  28. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
  29. void *parent_tidptr, void *tls, void *child_tidptr) */
  30. ENTRY (__clone)
  31. /* Align stack to 16. */
  32. REG_ALIGN_ASM (a1, 4)
  33. /* Sanity check arguments. */
  34. beq a0, zero, L (invalid) /* No NULL function pointers. */
  35. beq a1, zero, L (invalid) /* No NULL stack pointers. */
  36. ADDI a1, a1, -STACK_ALIGN /* Reserve argument save space. */
  37. REG_S a0, a1, 0 /* Save function pointer. */
  38. REG_S a3, a1, SZREG /* Save argument pointer. */
  39. /* The syscall expects the args to be in different slots. */
  40. or a0, a2, zero
  41. or a2, a4, zero
  42. or a3, a6, zero
  43. or a4, a5, zero
  44. /* Do the system call. */
  45. LI a7,__NR_clone
  46. syscall 0
  47. blt a0, zero ,L (error)
  48. beq a0, zero, L (thread_start)
  49. /* Successful return from the parent. */
  50. ret
  51. L (invalid):
  52. LI a0, -EINVAL
  53. /* Something bad happened -- no child created. */
  54. L (error):
  55. b __syscall_error
  56. END (__clone)
  57. /* Load up the arguments to the function. Put this block of code in
  58. its own function so that we can terminate the stack trace with our
  59. debug info. */
  60. ENTRY (__thread_start)
  61. L (thread_start):
  62. /* Restore the arg for user's function. */
  63. REG_L a1, sp, 0 /* Function pointer. */
  64. REG_L a0, sp, SZREG /* Argument pointer. */
  65. /* Call the user's function. */
  66. jirl ra, a1, 0
  67. /* Call exit with the function's return value. */
  68. LI a7, __NR_exit
  69. syscall 0
  70. END (__thread_start)
  71. libc_hidden_def (__clone)
  72. weak_alias (__clone, clone)