clone.S 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* clone() implementation for Nios II.
  2. Copyright (C) 2008-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Andrew Jenner <andrew@codesourcery.com>, 2008.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. /* clone() is even more special than fork() as it mucks with stacks
  17. and invokes a function in the right context after its all over. */
  18. #include <sysdep.h>
  19. #define _ERRNO_H 1
  20. #include <bits/errno.h>
  21. #include <tcb-offsets.h>
  22. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
  23. void *parent_tidptr, void *tls, void *child_tidptr) */
  24. .text
  25. ENTRY(__clone)
  26. /* Sanity check arguments. */
  27. movi r2, EINVAL
  28. /* No NULL function pointers. */
  29. beq r4, zero, SYSCALL_ERROR_LABEL
  30. /* No NULL stack pointers. */
  31. beq r5, zero, SYSCALL_ERROR_LABEL
  32. subi r5, r5, 8 /* Reserve argument save space. */
  33. stw r4, 4(r5) /* Save function pointer. */
  34. stw r7, 0(r5) /* Save argument pointer. */
  35. /* Load arguments. */
  36. mov r4, r6
  37. ldw r6, 0(sp)
  38. ldw r7, 8(sp)
  39. ldw r8, 4(sp)
  40. /* Do the system call. */
  41. movi r2, SYS_ify (clone)
  42. /* End FDE now, because in the child the unwind info will be
  43. wrong. */
  44. cfi_endproc
  45. trap
  46. /* Check for errors. */
  47. bne r7, zero, SYSCALL_ERROR_LABEL
  48. /* See if we're on the newly created thread. */
  49. beq r2, zero, thread_start
  50. /* Successful return from the parent */
  51. ret
  52. thread_start:
  53. cfi_startproc
  54. cfi_undefined (ra)
  55. ldw r5, 4(sp) /* Function pointer. */
  56. ldw r4, 0(sp) /* Argument pointer. */
  57. addi sp, sp, 8
  58. /* Call the user's function. */
  59. callr r5
  60. /* exit with the result. */
  61. movi r2, SYS_ify (exit)
  62. trap
  63. cfi_endproc
  64. cfi_startproc
  65. PSEUDO_END (__clone)
  66. libc_hidden_def (__clone)
  67. weak_alias (__clone, clone)