sysdep-cancel.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* Copyright (C) 2003, 2004, 2005, 2009 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, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <sysdep.h>
  15. #include <tcb-offsets.h>
  16. #ifndef __ASSEMBLER__
  17. # include <pthreadP.h>
  18. #endif
  19. #if !defined NOT_IN_libc || defined IS_IN_libpthread || defined IS_IN_librt
  20. /* NOTE: We do mark syscalls with unwind annotations, for the benefit of
  21. cancellation; but they're really only accurate at the point of the
  22. syscall. The ARM unwind directives are not rich enough without adding
  23. a custom personality function. */
  24. # undef PSEUDO
  25. # define PSEUDO(name, syscall_name, args) \
  26. .section ".text"; \
  27. PSEUDO_PROLOGUE; \
  28. .type __##syscall_name##_nocancel,%function; \
  29. .globl __##syscall_name##_nocancel; \
  30. __##syscall_name##_nocancel: \
  31. cfi_sections(.debug_frame); \
  32. cfi_startproc; \
  33. DO_CALL (syscall_name, args); \
  34. PSEUDO_RET; \
  35. cfi_endproc; \
  36. .size __##syscall_name##_nocancel,.-__##syscall_name##_nocancel; \
  37. ENTRY (name); \
  38. SINGLE_THREAD_P; \
  39. DOARGS_##args; \
  40. bne .Lpseudo_cancel; \
  41. cfi_remember_state; \
  42. DO_CALL (syscall_name, 0); \
  43. UNDOARGS_##args; \
  44. cmn r0, $4096; \
  45. PSEUDO_RET; \
  46. cfi_restore_state; \
  47. .Lpseudo_cancel: \
  48. .fnstart; \
  49. DOCARGS_##args; /* save syscall args etc. around CENABLE. */ \
  50. CENABLE; \
  51. mov ip, r0; /* put mask in safe place. */ \
  52. UNDOCARGS_##args; /* restore syscall args. */ \
  53. ldr r7, =SYS_ify (syscall_name); \
  54. swi 0x0; /* do the call. */ \
  55. .fnend; /* Past here we can't easily unwind. */ \
  56. mov r7, r0; /* save syscall return value. */ \
  57. mov r0, ip; /* get mask back. */ \
  58. CDISABLE; \
  59. mov r0, r7; /* retrieve return value. */ \
  60. RESTORE_LR_##args; \
  61. UNDOARGS_##args; \
  62. cmn r0, $4096
  63. /* DOARGS pushes four bytes on the stack for five arguments, eight bytes for
  64. six arguments, and nothing for fewer. In order to preserve doubleword
  65. alignment, sometimes we must save an extra register. */
  66. # define RESTART_UNWIND \
  67. .fnend; \
  68. .fnstart; \
  69. .save {r7, lr}
  70. # define DOCARGS_0 \
  71. stmfd sp!, {r7, lr}; \
  72. cfi_adjust_cfa_offset (8); \
  73. cfi_rel_offset (r7, 0); \
  74. cfi_rel_offset (lr, 4); \
  75. .save {r7, lr}
  76. # define UNDOCARGS_0
  77. # define RESTORE_LR_0 \
  78. ldmfd sp!, {r7, lr}; \
  79. cfi_adjust_cfa_offset (-8); \
  80. cfi_restore (r7); \
  81. cfi_restore (lr)
  82. # define DOCARGS_1 \
  83. stmfd sp!, {r0, r1, r7, lr}; \
  84. cfi_adjust_cfa_offset (16); \
  85. cfi_rel_offset (r7, 8); \
  86. cfi_rel_offset (lr, 12); \
  87. .save {r7, lr}; \
  88. .pad #8
  89. # define UNDOCARGS_1 \
  90. ldr r0, [sp], #8; \
  91. cfi_adjust_cfa_offset (-8); \
  92. RESTART_UNWIND
  93. # define RESTORE_LR_1 \
  94. RESTORE_LR_0
  95. # define DOCARGS_2 \
  96. stmfd sp!, {r0, r1, r7, lr}; \
  97. cfi_adjust_cfa_offset (16); \
  98. cfi_rel_offset (r7, 8); \
  99. cfi_rel_offset (lr, 12); \
  100. .save {r7, lr}; \
  101. .pad #8
  102. # define UNDOCARGS_2 \
  103. ldmfd sp!, {r0, r1}; \
  104. cfi_adjust_cfa_offset (-8); \
  105. RESTART_UNWIND
  106. # define RESTORE_LR_2 \
  107. RESTORE_LR_0
  108. # define DOCARGS_3 \
  109. stmfd sp!, {r0, r1, r2, r3, r7, lr}; \
  110. cfi_adjust_cfa_offset (24); \
  111. cfi_rel_offset (r7, 16); \
  112. cfi_rel_offset (lr, 20); \
  113. .save {r7, lr}; \
  114. .pad #16
  115. # define UNDOCARGS_3 \
  116. ldmfd sp!, {r0, r1, r2, r3}; \
  117. cfi_adjust_cfa_offset (-16); \
  118. RESTART_UNWIND
  119. # define RESTORE_LR_3 \
  120. RESTORE_LR_0
  121. # define DOCARGS_4 \
  122. stmfd sp!, {r0, r1, r2, r3, r7, lr}; \
  123. cfi_adjust_cfa_offset (24); \
  124. cfi_rel_offset (r7, 16); \
  125. cfi_rel_offset (lr, 20); \
  126. .save {r7, lr}; \
  127. .pad #16
  128. # define UNDOCARGS_4 \
  129. ldmfd sp!, {r0, r1, r2, r3}; \
  130. cfi_adjust_cfa_offset (-16); \
  131. RESTART_UNWIND
  132. # define RESTORE_LR_4 \
  133. RESTORE_LR_0
  134. /* r4 is only stmfd'ed for correct stack alignment. */
  135. # define DOCARGS_5 \
  136. .save {r4}; \
  137. stmfd sp!, {r0, r1, r2, r3, r4, r7, lr}; \
  138. cfi_adjust_cfa_offset (28); \
  139. cfi_rel_offset (r7, 20); \
  140. cfi_rel_offset (lr, 24); \
  141. .save {r7, lr}; \
  142. .pad #20
  143. # define UNDOCARGS_5 \
  144. ldmfd sp!, {r0, r1, r2, r3}; \
  145. cfi_adjust_cfa_offset (-16); \
  146. .fnend; \
  147. .fnstart; \
  148. .save {r4}; \
  149. .save {r7, lr}; \
  150. .pad #4
  151. # define RESTORE_LR_5 \
  152. ldmfd sp!, {r4, r7, lr}; \
  153. cfi_adjust_cfa_offset (-12); \
  154. /* r4 will be marked as restored later. */ \
  155. cfi_restore (r7); \
  156. cfi_restore (lr)
  157. # define DOCARGS_6 \
  158. .save {r4, r5}; \
  159. stmfd sp!, {r0, r1, r2, r3, r7, lr}; \
  160. cfi_adjust_cfa_offset (24); \
  161. cfi_rel_offset (r7, 16); \
  162. cfi_rel_offset (lr, 20); \
  163. .save {r7, lr}; \
  164. .pad #16
  165. # define UNDOCARGS_6 \
  166. ldmfd sp!, {r0, r1, r2, r3}; \
  167. cfi_adjust_cfa_offset (-16); \
  168. .fnend; \
  169. .fnstart; \
  170. .save {r4, r5}; \
  171. .save {r7, lr}
  172. # define RESTORE_LR_6 \
  173. RESTORE_LR_0
  174. # ifdef IS_IN_libpthread
  175. # define CENABLE bl PLTJMP(__pthread_enable_asynccancel)
  176. # define CDISABLE bl PLTJMP(__pthread_disable_asynccancel)
  177. # define __local_multiple_threads __pthread_multiple_threads
  178. # elif !defined NOT_IN_libc
  179. # define CENABLE bl PLTJMP(__libc_enable_asynccancel)
  180. # define CDISABLE bl PLTJMP(__libc_disable_asynccancel)
  181. # define __local_multiple_threads __libc_multiple_threads
  182. # elif defined IS_IN_librt
  183. # define CENABLE bl PLTJMP(__librt_enable_asynccancel)
  184. # define CDISABLE bl PLTJMP(__librt_disable_asynccancel)
  185. # else
  186. # error Unsupported library
  187. # endif
  188. # if defined IS_IN_libpthread || !defined NOT_IN_libc
  189. # ifndef __ASSEMBLER__
  190. extern int __local_multiple_threads attribute_hidden;
  191. # define SINGLE_THREAD_P __builtin_expect (__local_multiple_threads == 0, 1)
  192. # else
  193. # define SINGLE_THREAD_P \
  194. ldr ip, 1b; \
  195. 2: \
  196. ldr ip, [pc, ip]; \
  197. teq ip, #0;
  198. # define PSEUDO_PROLOGUE \
  199. 1: .word __local_multiple_threads - 2f - 8;
  200. # endif
  201. # else
  202. /* There is no __local_multiple_threads for librt, so use the TCB. */
  203. # ifndef __ASSEMBLER__
  204. # define SINGLE_THREAD_P \
  205. __builtin_expect (THREAD_GETMEM (THREAD_SELF, \
  206. header.multiple_threads) == 0, 1)
  207. # else
  208. # define PSEUDO_PROLOGUE
  209. # define SINGLE_THREAD_P \
  210. stmfd sp!, {r0, lr}; \
  211. cfi_adjust_cfa_offset (8); \
  212. cfi_rel_offset (lr, 4); \
  213. bl __aeabi_read_tp; \
  214. ldr ip, [r0, #MULTIPLE_THREADS_OFFSET]; \
  215. ldmfd sp!, {r0, lr}; \
  216. cfi_adjust_cfa_offset (-8); \
  217. cfi_restore (lr); \
  218. teq ip, #0
  219. # define SINGLE_THREAD_P_PIC(x) SINGLE_THREAD_P
  220. # endif
  221. # endif
  222. #elif !defined __ASSEMBLER__
  223. /* For rtld, et cetera. */
  224. # define SINGLE_THREAD_P 1
  225. # define NO_CANCELLATION 1
  226. #endif
  227. #ifndef __ASSEMBLER__
  228. # define RTLD_SINGLE_THREAD_P \
  229. __builtin_expect (THREAD_GETMEM (THREAD_SELF, \
  230. header.multiple_threads) == 0, 1)
  231. #endif