clone.S 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #define CLONE_VM 0x00000100
  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. beq r4, zero, .Lerror /* No NULL function pointers. */
  29. beq r5, zero, .Lerror /* No NULL stack pointers. */
  30. subi r5, r5, 8 /* Reserve argument save space. */
  31. stw r4, 4(r5) /* Save function pointer. */
  32. stw r7, 0(r5) /* Save argument pointer. */
  33. /* Load arguments. */
  34. mov r4, r6
  35. ldw r6, 0(sp)
  36. ldw r7, 8(sp)
  37. ldw r8, 4(sp)
  38. /* Do the system call. */
  39. movi r2, SYS_ify (clone)
  40. trap
  41. /* Check for error: nios2 flags it in r7 and returns the errno in r2. */
  42. bne r7, zero, .Lerror
  43. /* See if we're on the newly created thread. */
  44. beq r2, zero, thread_start
  45. /* Successful return from the parent */
  46. ret
  47. .Lerror:
  48. /* __syscall_error expects the negated error number. */
  49. sub r4, zero, r2
  50. #ifdef __PIC__
  51. nextpc r22
  52. 1: movhi r8, %hiadj(_gp_got - 1b)
  53. addi r8, r8, %lo(_gp_got - 1b)
  54. add r22, r22, r8
  55. ldw r8, %call(__syscall_error)(r22)
  56. jmp r8
  57. #else
  58. jmpi __syscall_error
  59. #endif
  60. thread_start:
  61. /* We expect the argument registers to be preserved across system
  62. calls and across task cloning, so flags should be in r4 here. */
  63. andi r2, r4, CLONE_VM
  64. bne r2, zero, 2f
  65. DO_CALL (getpid, 0)
  66. 2:
  67. ldw r5, 4(sp) /* Function pointer. */
  68. ldw r4, 0(sp) /* Argument pointer. */
  69. addi sp, sp, 8
  70. /* Call the user's function. */
  71. callr r5
  72. /* _exit with the result. */
  73. mov r4, r2
  74. #ifdef __PIC__
  75. nextpc r22
  76. 1: movhi r8, %hiadj(_gp_got - 1b)
  77. addi r8, r8, %lo(_gp_got - 1b)
  78. add r22, r22, r8
  79. ldw r8, %call(HIDDEN_JUMPTARGET(_exit))(r22)
  80. jmp r8
  81. #else
  82. jmpi _exit
  83. #endif
  84. END(__clone)
  85. libc_hidden_def (__clone)
  86. weak_alias (__clone, clone)