clone.S 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /* Sanity check arguments. */
  25. beqz a0,L (invalid) /* No NULL function pointers. */
  26. beqz a1,L (invalid) /* No NULL stack pointers. */
  27. addi a1,a1,-16 /* Reserve argument save space. */
  28. REG_S a0,0(a1) /* Save function pointer. */
  29. REG_S a3,SZREG(a1) /* Save argument pointer. */
  30. /* The syscall expects the args to be in different slots. */
  31. mv a0,a2
  32. mv a2,a4
  33. mv a3,a5
  34. mv a4,a6
  35. /* Do the system call. */
  36. li a7,__NR_clone
  37. scall
  38. bltz a0,L (error)
  39. beqz a0,L (thread_start)
  40. /* Successful return from the parent. */
  41. ret
  42. L (invalid):
  43. li a0, -EINVAL
  44. /* Something bad happened -- no child created. */
  45. L (error):
  46. j __syscall_error
  47. END (clone)
  48. /* Load up the arguments to the function. Put this block of code in
  49. its own function so that we can terminate the stack trace with our
  50. debug info. */
  51. ENTRY (__thread_start)
  52. L (thread_start):
  53. /* Restore the arg for user's function. */
  54. REG_L a1,0(sp) /* Function pointer. */
  55. REG_L a0,SZREG(sp) /* Argument pointer. */
  56. /* Call the user's function. */
  57. jalr a1
  58. /* Call exit with the function's return value. */
  59. li a7, __NR_exit
  60. scall
  61. END (__thread_start)