clone.S 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Wrapper around clone system call. LoongArch64 version.
  2. Copyright (C) 2024 uClibc-ng contributors.
  3. Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  4. */
  5. /* clone() is even more special than fork() as it mucks with stacks
  6. and invokes a function in the right context after its all over. */
  7. #include <sys/asm.h>
  8. #include <sysdep.h>
  9. #define _ERRNO_H 1
  10. #include <bits/errno.h>
  11. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
  12. void *parent_tidptr, void *tls, void *child_tidptr) */
  13. .text
  14. LEAF (clone)
  15. /* Align stack to a 16-byte boundary as per LoongArch ABI. */
  16. bstrins.d $a1, $zero, 3, 0
  17. /* Sanity check arguments. */
  18. beqz $a0, L(invalid) /* No NULL function pointers. */
  19. beqz $a1, L(invalid) /* No NULL stack pointers. */
  20. addi.d $a1, $a1, -16 /* Reserve argument save space. */
  21. st.d $a0, $a1, 0 /* Save function pointer. */
  22. st.d $a3, $a1, 8 /* Save argument pointer. */
  23. /* The syscall expects the args to be in different slots. */
  24. or $a0, $a2, $zero
  25. or $a2, $a4, $zero
  26. or $a3, $a6, $zero
  27. or $a4, $a5, $zero
  28. /* Do the system call. */
  29. li.w $a7, __NR_clone
  30. syscall 0
  31. blt $a0, $zero, L(error)
  32. beqz $a0, L(thread_start)
  33. /* Successful return from the parent. */
  34. jr $ra
  35. L(invalid):
  36. li.w $a0, -EINVAL
  37. /* Something bad happened -- no child created. */
  38. L(error):
  39. b __syscall_error
  40. END (clone)
  41. /* Load up the arguments to the function. Put this block of code in
  42. its own function so that we can terminate the stack trace with our
  43. debug info. */
  44. ENTRY (__thread_start)
  45. L(thread_start):
  46. /* Restore the arg for user's function. */
  47. ld.d $t0, $sp, 0 /* Function pointer. */
  48. ld.d $a0, $sp, 8 /* Argument pointer. */
  49. /* Call the user's function. */
  50. jirl $ra, $t0, 0
  51. /* Call exit with the function's return value. */
  52. li.w $a7, __NR_exit
  53. syscall 0
  54. END (__thread_start)