clone.S 2.4 KB

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