clone.S 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* clone() implementation for Nios II.
  2. Copyright (C) 2008-2016 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. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  22. #include <tcb-offsets.h>
  23. #endif
  24. #define CLONE_VM 0x00000100
  25. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
  26. void *parent_tidptr, void *tls, void *child_tidptr) */
  27. .text
  28. ENTRY(__clone)
  29. /* Sanity check arguments. */
  30. movi r2, EINVAL
  31. subi r5, r5, 8 /* Reserve argument save space. */
  32. stw r4, 4(r5) /* Save function pointer. */
  33. stw r7, 0(r5) /* Save argument pointer. */
  34. /* Load arguments. */
  35. mov r4, r6
  36. ldw r6, 0(sp)
  37. ldw r7, 8(sp)
  38. ldw r8, 4(sp)
  39. /* Do the system call. */
  40. movi r2, SYS_ify (clone)
  41. trap
  42. /* See if we're on the newly created thread. */
  43. beq r2, zero, thread_start
  44. /* Successful return from the parent */
  45. ret
  46. thread_start:
  47. /* We expect the argument registers to be preserved across system
  48. calls and across task cloning, so flags should be in r4 here. */
  49. andi r2, r4, CLONE_VM
  50. bne r2, zero, 2f
  51. DO_CALL (getpid, 0)
  52. #ifdef RESET_PID
  53. stw r2, PID_OFFSET(r23)
  54. stw r2, TID_OFFSET(r23)
  55. #endif
  56. 2:
  57. ldw r5, 4(sp) /* Function pointer. */
  58. ldw r4, 0(sp) /* Argument pointer. */
  59. addi sp, sp, 8
  60. /* Call the user's function. */
  61. callr r5
  62. /* _exit with the result. */
  63. mov r4, r2
  64. #ifdef __PIC__
  65. nextpc r22
  66. 1: movhi r8, %hiadj(_gp_got - 1b)
  67. addi r8, r8, %lo(_gp_got - 1b)
  68. add r22, r22, r8
  69. ldw r8, %call(HIDDEN_JUMPTARGET(_exit))(r22)
  70. jmp r8
  71. #else
  72. jmpi _exit
  73. #endif
  74. END(__clone)
  75. libc_hidden_def (__clone)
  76. weak_alias (__clone, clone)