clone.S 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Wrapper around clone system call. RISC-V version.
  2. Copyright (C) 1996-2018 Free Software Foundation, Inc.
  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 <sys/asm.h>
  17. #include <sysdep.h>
  18. #define _ERRNO_H 1
  19. #include <bits/errno.h>
  20. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
  21. void *parent_tidptr, void *tls, void *child_tidptr) */
  22. .text
  23. LEAF (clone)
  24. /* Align stack to a 128-bit boundary as per RISC-V ABI. */
  25. andi a1,a1,ALMASK
  26. /* Sanity check arguments. */
  27. beqz a0,L (invalid) /* No NULL function pointers. */
  28. beqz a1,L (invalid) /* No NULL stack pointers. */
  29. addi a1,a1,-16 /* Reserve argument save space. */
  30. REG_S a0,0(a1) /* Save function pointer. */
  31. REG_S a3,SZREG(a1) /* Save argument pointer. */
  32. /* The syscall expects the args to be in different slots. */
  33. mv a0,a2
  34. mv a2,a4
  35. mv a3,a5
  36. mv a4,a6
  37. /* Do the system call. */
  38. li a7,__NR_clone
  39. scall
  40. bltz a0,L (error)
  41. beqz a0,L (thread_start)
  42. /* Successful return from the parent. */
  43. ret
  44. L (invalid):
  45. li a0, -EINVAL
  46. /* Something bad happened -- no child created. */
  47. L (error):
  48. tail __syscall_error
  49. END (clone)
  50. /* Load up the arguments to the function. Put this block of code in
  51. its own function so that we can terminate the stack trace with our
  52. debug info. */
  53. ENTRY (__thread_start)
  54. L (thread_start):
  55. .cfi_label .Ldummy
  56. cfi_undefined (ra)
  57. /* Restore the arg for user's function. */
  58. REG_L a1,0(sp) /* Function pointer. */
  59. REG_L a0,SZREG(sp) /* Argument pointer. */
  60. /* Call the user's function. */
  61. jalr a1
  62. /* Call exit with the function's return value. */
  63. li a7, __NR_exit
  64. scall
  65. END (__thread_start)