sysdep.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /* Copyright (C) 1992-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Brendan Kehoe (brendan@zen.org).
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library. If not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <sysdeps/unix/sysdep.h>
  16. #include <dl-sysdep.h> /* Defines RTLD_PRIVATE_ERRNO. */
  17. #ifdef __ASSEMBLER__
  18. #ifdef __linux__
  19. # include <alpha/regdef.h>
  20. #else
  21. # include <regdef.h>
  22. #endif
  23. #define __LABEL(x) x##:
  24. #define LEAF(name, framesize) \
  25. .globl name; \
  26. .align 4; \
  27. .ent name, 0; \
  28. __LABEL(name) \
  29. .frame sp, framesize, ra
  30. #define ENTRY(name) \
  31. .globl name; \
  32. .align 4; \
  33. .ent name, 0; \
  34. __LABEL(name) \
  35. .frame sp, 0, ra
  36. /* Mark the end of function SYM. */
  37. #undef END
  38. #define END(sym) .end sym
  39. #ifdef PROF
  40. # define PSEUDO_PROF \
  41. .set noat; \
  42. lda AT, _mcount; \
  43. jsr AT, (AT), _mcount; \
  44. .set at
  45. #else
  46. # define PSEUDO_PROF
  47. #endif
  48. #ifdef PROF
  49. # define PSEUDO_PROLOGUE \
  50. .frame sp, 0, ra; \
  51. ldgp gp,0(pv); \
  52. PSEUDO_PROF; \
  53. .prologue 1
  54. #elif defined PIC
  55. # define PSEUDO_PROLOGUE \
  56. .frame sp, 0, ra; \
  57. .prologue 0
  58. #else
  59. # define PSEUDO_PROLOGUE \
  60. .frame sp, 0, ra; \
  61. ldgp gp,0(pv); \
  62. .prologue 1
  63. #endif /* PROF */
  64. #ifdef PROF
  65. # define USEPV_PROF std
  66. #else
  67. # define USEPV_PROF no
  68. #endif
  69. #if RTLD_PRIVATE_ERRNO
  70. # define SYSCALL_ERROR_LABEL $syscall_error
  71. # define SYSCALL_ERROR_HANDLER \
  72. $syscall_error: \
  73. stl v0, rtld_errno(gp) !gprel; \
  74. lda v0, -1; \
  75. ret
  76. # define SYSCALL_ERROR_FALLTHRU
  77. #elif defined(PIC)
  78. # define SYSCALL_ERROR_LABEL __syscall_error !samegp
  79. # define SYSCALL_ERROR_HANDLER
  80. # define SYSCALL_ERROR_FALLTHRU br SYSCALL_ERROR_LABEL
  81. #else
  82. # define SYSCALL_ERROR_LABEL $syscall_error
  83. # define SYSCALL_ERROR_HANDLER \
  84. $syscall_error: \
  85. jmp $31, __syscall_error
  86. # define SYSCALL_ERROR_FALLTHRU
  87. #endif /* RTLD_PRIVATE_ERRNO */
  88. /* Overridden by specific syscalls. */
  89. #undef PSEUDO_PREPARE_ARGS
  90. #define PSEUDO_PREPARE_ARGS /* Nothing. */
  91. #define PSEUDO(name, syscall_name, args) \
  92. .globl name; \
  93. .align 4; \
  94. .ent name,0; \
  95. __LABEL(name) \
  96. PSEUDO_PROLOGUE; \
  97. PSEUDO_PREPARE_ARGS \
  98. lda v0, SYS_ify(syscall_name); \
  99. call_pal PAL_callsys; \
  100. bne a3, SYSCALL_ERROR_LABEL
  101. #undef PSEUDO_END
  102. #define PSEUDO_END(sym) \
  103. SYSCALL_ERROR_HANDLER; \
  104. END(sym)
  105. #define PSEUDO_NOERRNO(name, syscall_name, args) \
  106. .globl name; \
  107. .align 4; \
  108. .ent name,0; \
  109. __LABEL(name) \
  110. PSEUDO_PROLOGUE; \
  111. PSEUDO_PREPARE_ARGS \
  112. lda v0, SYS_ify(syscall_name); \
  113. call_pal PAL_callsys;
  114. #undef PSEUDO_END_NOERRNO
  115. #define PSEUDO_END_NOERRNO(sym) END(sym)
  116. #define ret_NOERRNO ret
  117. #define PSEUDO_ERRVAL(name, syscall_name, args) \
  118. .globl name; \
  119. .align 4; \
  120. .ent name,0; \
  121. __LABEL(name) \
  122. PSEUDO_PROLOGUE; \
  123. PSEUDO_PREPARE_ARGS \
  124. lda v0, SYS_ify(syscall_name); \
  125. call_pal PAL_callsys;
  126. #undef PSEUDO_END_ERRVAL
  127. #define PSEUDO_END_ERRVAL(sym) END(sym)
  128. #define ret_ERRVAL ret
  129. #define r0 v0
  130. #define r1 a4
  131. #define MOVE(x,y) mov x,y
  132. #else /* !ASSEMBLER */
  133. /* In order to get __set_errno() definition in INLINE_SYSCALL. */
  134. #include <errno.h>
  135. /* ??? Linux needs to be able to override INLINE_SYSCALL for one
  136. particular special case. Make this easy. */
  137. #undef INLINE_SYSCALL
  138. #define INLINE_SYSCALL(name, nr, args...) \
  139. INLINE_SYSCALL1(name, nr, args)
  140. #define INLINE_SYSCALL1(name, nr, args...) \
  141. ({ \
  142. long _sc_ret, _sc_err; \
  143. inline_syscall##nr(__NR_##name, args); \
  144. if (__builtin_expect (_sc_err, 0)) \
  145. { \
  146. __set_errno (_sc_ret); \
  147. _sc_ret = -1L; \
  148. } \
  149. _sc_ret; \
  150. })
  151. #define INTERNAL_SYSCALL(name, err_out, nr, args...) \
  152. INTERNAL_SYSCALL1(name, err_out, nr, args)
  153. #define INTERNAL_SYSCALL1(name, err_out, nr, args...) \
  154. INTERNAL_SYSCALL_NCS(__NR_##name, err_out, nr, args)
  155. #define INTERNAL_SYSCALL_NCS(name, err_out, nr, args...) \
  156. ({ \
  157. long _sc_ret, _sc_err; \
  158. inline_syscall##nr(name, args); \
  159. err_out = _sc_err; \
  160. _sc_ret; \
  161. })
  162. #define INTERNAL_SYSCALL_DECL(err) \
  163. long int err __attribute__((unused))
  164. /* The normal Alpha calling convention sign-extends 32-bit quantties
  165. no matter what the "real" sign of the 32-bit type. We want to
  166. preserve that when filling in values for the kernel. */
  167. #define syscall_promote(arg) \
  168. (sizeof(arg) == 4 ? (long)(int)(long)(arg) : (long)(arg))
  169. /* Make sure and "use" the variable that we're not returning,
  170. in order to suppress unused variable warnings. */
  171. #define INTERNAL_SYSCALL_ERROR_P(val, err) ((void)val, err)
  172. #define INTERNAL_SYSCALL_ERRNO(val, err) ((void)err, val)
  173. #define inline_syscall_clobbers \
  174. "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8", \
  175. "$22", "$23", "$24", "$25", "$27", "$28", "memory"
  176. /* It is moderately important optimization-wise to limit the lifetime
  177. of the hard-register variables as much as possible. Thus we copy
  178. in/out as close to the asm as possible. */
  179. #define inline_syscall0(name, args...) \
  180. { \
  181. register long _sc_19 __asm__("$19"); \
  182. register long _sc_0 = name; \
  183. __asm__ __volatile__ \
  184. ("callsys # %0 %1 <= %2" \
  185. : "+v"(_sc_0), "=r"(_sc_19) \
  186. : : inline_syscall_clobbers, \
  187. "$16", "$17", "$18", "$20", "$21"); \
  188. _sc_ret = _sc_0, _sc_err = _sc_19; \
  189. }
  190. #define inline_syscall1(name,arg1) \
  191. { \
  192. register long _tmp_16 = syscall_promote (arg1); \
  193. register long _sc_0 = name; \
  194. register long _sc_16 __asm__("$16") = _tmp_16; \
  195. register long _sc_19 __asm__("$19"); \
  196. __asm__ __volatile__ \
  197. ("callsys # %0 %1 <= %2 %3" \
  198. : "+v"(_sc_0), "=r"(_sc_19), "+r"(_sc_16) \
  199. : : inline_syscall_clobbers, \
  200. "$17", "$18", "$20", "$21"); \
  201. _sc_ret = _sc_0, _sc_err = _sc_19; \
  202. }
  203. #define inline_syscall2(name,arg1,arg2) \
  204. { \
  205. register long _tmp_16 = syscall_promote (arg1); \
  206. register long _tmp_17 = syscall_promote (arg2); \
  207. register long _sc_0 = name; \
  208. register long _sc_16 __asm__("$16") = _tmp_16; \
  209. register long _sc_17 __asm__("$17") = _tmp_17; \
  210. register long _sc_19 __asm__("$19"); \
  211. __asm__ __volatile__ \
  212. ("callsys # %0 %1 <= %2 %3 %4" \
  213. : "+v"(_sc_0), "=r"(_sc_19), \
  214. "+r"(_sc_16), "+r"(_sc_17) \
  215. : : inline_syscall_clobbers, \
  216. "$18", "$20", "$21"); \
  217. _sc_ret = _sc_0, _sc_err = _sc_19; \
  218. }
  219. #define inline_syscall3(name,arg1,arg2,arg3) \
  220. { \
  221. register long _tmp_16 = syscall_promote (arg1); \
  222. register long _tmp_17 = syscall_promote (arg2); \
  223. register long _tmp_18 = syscall_promote (arg3); \
  224. register long _sc_0 = name; \
  225. register long _sc_16 __asm__("$16") = _tmp_16; \
  226. register long _sc_17 __asm__("$17") = _tmp_17; \
  227. register long _sc_18 __asm__("$18") = _tmp_18; \
  228. register long _sc_19 __asm__("$19"); \
  229. __asm__ __volatile__ \
  230. ("callsys # %0 %1 <= %2 %3 %4 %5" \
  231. : "+v"(_sc_0), "=r"(_sc_19), "+r"(_sc_16), \
  232. "+r"(_sc_17), "+r"(_sc_18) \
  233. : : inline_syscall_clobbers, "$20", "$21"); \
  234. _sc_ret = _sc_0, _sc_err = _sc_19; \
  235. }
  236. #define inline_syscall4(name,arg1,arg2,arg3,arg4) \
  237. { \
  238. register long _tmp_16 = syscall_promote (arg1); \
  239. register long _tmp_17 = syscall_promote (arg2); \
  240. register long _tmp_18 = syscall_promote (arg3); \
  241. register long _tmp_19 = syscall_promote (arg4); \
  242. register long _sc_0 = name; \
  243. register long _sc_16 __asm__("$16") = _tmp_16; \
  244. register long _sc_17 __asm__("$17") = _tmp_17; \
  245. register long _sc_18 __asm__("$18") = _tmp_18; \
  246. register long _sc_19 __asm__("$19") = _tmp_19; \
  247. __asm__ __volatile__ \
  248. ("callsys # %0 %1 <= %2 %3 %4 %5 %6" \
  249. : "+v"(_sc_0), "+r"(_sc_19), "+r"(_sc_16), \
  250. "+r"(_sc_17), "+r"(_sc_18) \
  251. : : inline_syscall_clobbers, "$20", "$21"); \
  252. _sc_ret = _sc_0, _sc_err = _sc_19; \
  253. }
  254. #define inline_syscall5(name,arg1,arg2,arg3,arg4,arg5) \
  255. { \
  256. register long _tmp_16 = syscall_promote (arg1); \
  257. register long _tmp_17 = syscall_promote (arg2); \
  258. register long _tmp_18 = syscall_promote (arg3); \
  259. register long _tmp_19 = syscall_promote (arg4); \
  260. register long _tmp_20 = syscall_promote (arg5); \
  261. register long _sc_0 = name; \
  262. register long _sc_16 __asm__("$16") = _tmp_16; \
  263. register long _sc_17 __asm__("$17") = _tmp_17; \
  264. register long _sc_18 __asm__("$18") = _tmp_18; \
  265. register long _sc_19 __asm__("$19") = _tmp_19; \
  266. register long _sc_20 __asm__("$20") = _tmp_20; \
  267. __asm__ __volatile__ \
  268. ("callsys # %0 %1 <= %2 %3 %4 %5 %6 %7" \
  269. : "+v"(_sc_0), "+r"(_sc_19), "+r"(_sc_16), \
  270. "+r"(_sc_17), "+r"(_sc_18), "+r"(_sc_20) \
  271. : : inline_syscall_clobbers, "$21"); \
  272. _sc_ret = _sc_0, _sc_err = _sc_19; \
  273. }
  274. #define inline_syscall6(name,arg1,arg2,arg3,arg4,arg5,arg6) \
  275. { \
  276. register long _tmp_16 = syscall_promote (arg1); \
  277. register long _tmp_17 = syscall_promote (arg2); \
  278. register long _tmp_18 = syscall_promote (arg3); \
  279. register long _tmp_19 = syscall_promote (arg4); \
  280. register long _tmp_20 = syscall_promote (arg5); \
  281. register long _tmp_21 = syscall_promote (arg6); \
  282. register long _sc_0 = name; \
  283. register long _sc_16 __asm__("$16") = _tmp_16; \
  284. register long _sc_17 __asm__("$17") = _tmp_17; \
  285. register long _sc_18 __asm__("$18") = _tmp_18; \
  286. register long _sc_19 __asm__("$19") = _tmp_19; \
  287. register long _sc_20 __asm__("$20") = _tmp_20; \
  288. register long _sc_21 __asm__("$21") = _tmp_21; \
  289. __asm__ __volatile__ \
  290. ("callsys # %0 %1 <= %2 %3 %4 %5 %6 %7 %8" \
  291. : "+v"(_sc_0), "+r"(_sc_19), "+r"(_sc_16), \
  292. "+r"(_sc_17), "+r"(_sc_18), "+r"(_sc_20), \
  293. "+r"(_sc_21) \
  294. : : inline_syscall_clobbers); \
  295. _sc_ret = _sc_0, _sc_err = _sc_19; \
  296. }
  297. #endif /* ASSEMBLER */
  298. /* Pointer mangling support. Note that tls access is slow enough that
  299. we don't deoptimize things by placing the pointer check value there. */
  300. #ifdef __ASSEMBLER__
  301. # if IS_IN (rtld)
  302. # define PTR_MANGLE(dst, src, tmp) \
  303. ldah tmp, __pointer_chk_guard_local($29) !gprelhigh; \
  304. ldq tmp, __pointer_chk_guard_local(tmp) !gprellow; \
  305. xor src, tmp, dst
  306. # define PTR_MANGLE2(dst, src, tmp) \
  307. xor src, tmp, dst
  308. # elif defined SHARED
  309. # define PTR_MANGLE(dst, src, tmp) \
  310. ldq tmp, __pointer_chk_guard; \
  311. xor src, tmp, dst
  312. # else
  313. # define PTR_MANGLE(dst, src, tmp) \
  314. ldq tmp, __pointer_chk_guard_local; \
  315. xor src, tmp, dst
  316. # endif
  317. # define PTR_MANGLE2(dst, src, tmp) \
  318. xor src, tmp, dst
  319. # define PTR_DEMANGLE(dst, tmp) PTR_MANGLE(dst, dst, tmp)
  320. # define PTR_DEMANGLE2(dst, tmp) PTR_MANGLE2(dst, dst, tmp)
  321. #else
  322. # include <stdint.h>
  323. # if (IS_IN (rtld) \
  324. || (!defined SHARED && (IS_IN (libc) \
  325. || IS_IN (libpthread))))
  326. extern uintptr_t __pointer_chk_guard_local attribute_relro attribute_hidden;
  327. # define PTR_MANGLE(var) \
  328. (var) = (__typeof (var)) ((uintptr_t) (var) ^ __pointer_chk_guard_local)
  329. # else
  330. extern uintptr_t __pointer_chk_guard attribute_relro;
  331. # define PTR_MANGLE(var) \
  332. (var) = (__typeof(var)) ((uintptr_t) (var) ^ __pointer_chk_guard)
  333. # endif
  334. # define PTR_DEMANGLE(var) PTR_MANGLE(var)
  335. #endif /* ASSEMBLER */