clone.S 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Copyright (C) 1999, 2000 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  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, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. /* clone() is even more special than fork() as it mucks with stacks
  16. and invokes a function in the right context after its all over. */
  17. #include <features.h>
  18. #include <asm/unistd.h>
  19. #define _ERRNO_H 1
  20. #include <bits/errno.h>
  21. #define _SYSCALL_H
  22. #include <bits/sysnum.h>
  23. #ifdef __HAVE_SHARED__
  24. #define PLTJMP(_x) _x##@PLT
  25. #else
  26. #define PLTJMP(_x) _x
  27. #endif
  28. /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg); */
  29. .text
  30. .text
  31. .align 4
  32. .type clone,@function
  33. .globl clone;
  34. clone:
  35. /* sanity check arguments. */
  36. tst r4, r4
  37. bt 0f
  38. tst r5, r5
  39. bf/s 1f
  40. mov #+__NR_clone, r3
  41. 0:
  42. bra __syscall_error
  43. mov #-EINVAL, r4
  44. 1:
  45. /* insert the args onto the new stack */
  46. mov.l r7, @-r5
  47. /* save the function pointer as the 0th element */
  48. mov.l r4, @-r5
  49. /* do the system call */
  50. mov r6, r4
  51. trapa #(__SH_SYSCALL_TRAP_BASE + 2)
  52. mov r0, r1
  53. #ifdef __CONFIG_SH2__
  54. // 12 arithmetic shifts for the crappy sh2, because shad doesn't exist!
  55. shar r1
  56. shar r1
  57. shar r1
  58. shar r1
  59. shar r1
  60. shar r1
  61. shar r1
  62. shar r1
  63. shar r1
  64. shar r1
  65. shar r1
  66. shar r1
  67. #else
  68. mov #-12, r2
  69. shad r2, r1
  70. #endif
  71. not r1, r1 // r1=0 means r0 = -1 to -4095
  72. tst r1, r1 // i.e. error in linux
  73. bf/s 2f
  74. tst r0, r0
  75. bra __syscall_error
  76. mov r0, r4
  77. 2:
  78. bt 3f
  79. rts
  80. nop
  81. 3:
  82. /* thread starts */
  83. mov.l @r15, r1
  84. jsr @r1
  85. mov.l @(4,r15), r4
  86. /* we are done, passing the return value through r0 */
  87. mov.l .L1, r1
  88. #ifdef __HAVE_SHARED__
  89. mov.l r12, @-r15
  90. sts.l pr, @-r15
  91. mov r0, r4
  92. mova .LG, r0 /* .LG from syscall_error.S */
  93. mov.l .LG, r12
  94. add r0, r12
  95. mova .L1, r0
  96. add r0, r1
  97. jsr @r1
  98. nop
  99. lds.l @r15+, pr
  100. rts
  101. mov.l @r15+, r12
  102. #else
  103. jmp @r1
  104. mov r0, r4
  105. #endif
  106. .align 2
  107. .L1:
  108. .long PLTJMP( HIDDEN_JUMPTARGET(_exit))
  109. .size clone,.-clone;
  110. #include "syscall_error.S"