sysdep.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* Copyright (C) 1996-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Written by Andreas Schwab, <schwab@issan.informatik.uni-dortmund.de>,
  4. December 1995.
  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. #include <sysdeps/unix/sysv/linux/sysdep.h>
  17. #include <tls.h>
  18. /* Defines RTLD_PRIVATE_ERRNO. */
  19. #include <dl-sysdep.h>
  20. /* For Linux we can use the system call table in the header file
  21. /usr/include/asm/unistd.h
  22. of the kernel. But these symbols do not follow the SYS_* syntax
  23. so we have to redefine the `SYS_ify' macro here. */
  24. #undef SYS_ify
  25. #define SYS_ify(syscall_name) __NR_##syscall_name
  26. #ifdef __ASSEMBLER__
  27. /* Linux uses a negative return value to indicate syscall errors, unlike
  28. most Unices, which use the condition codes' carry flag.
  29. Since version 2.1 the return value of a system call might be negative
  30. even if the call succeeded. E.g., the `lseek' system call might return
  31. a large offset. Therefore we must not anymore test for < 0, but test
  32. for a real error by making sure the value in %d0 is a real error
  33. number. Linus said he will make sure the no syscall returns a value
  34. in -1 .. -4095 as a valid result so we can savely test with -4095. */
  35. /* We don't want the label for the error handler to be visible in the symbol
  36. table when we define it here. */
  37. #ifdef PIC
  38. #define SYSCALL_ERROR_LABEL .Lsyscall_error
  39. #else
  40. #define SYSCALL_ERROR_LABEL __syscall_error
  41. #endif
  42. #undef PSEUDO
  43. #define PSEUDO(name, syscall_name, args) \
  44. .text; \
  45. ENTRY (name) \
  46. DO_CALL (syscall_name, args); \
  47. cmp.l &-4095, %d0; \
  48. jcc SYSCALL_ERROR_LABEL
  49. #undef PSEUDO_END
  50. #define PSEUDO_END(name) \
  51. SYSCALL_ERROR_HANDLER; \
  52. END (name)
  53. #undef PSEUDO_NOERRNO
  54. #define PSEUDO_NOERRNO(name, syscall_name, args) \
  55. .text; \
  56. ENTRY (name) \
  57. DO_CALL (syscall_name, args)
  58. #undef PSEUDO_END_NOERRNO
  59. #define PSEUDO_END_NOERRNO(name) \
  60. END (name)
  61. #define ret_NOERRNO rts
  62. /* The function has to return the error code. */
  63. #undef PSEUDO_ERRVAL
  64. #define PSEUDO_ERRVAL(name, syscall_name, args) \
  65. .text; \
  66. ENTRY (name) \
  67. DO_CALL (syscall_name, args); \
  68. negl %d0
  69. #undef PSEUDO_END_ERRVAL
  70. #define PSEUDO_END_ERRVAL(name) \
  71. END (name)
  72. #define ret_ERRVAL rts
  73. #ifdef PIC
  74. # if RTLD_PRIVATE_ERRNO
  75. # define SYSCALL_ERROR_HANDLER \
  76. SYSCALL_ERROR_LABEL: \
  77. PCREL_OP (lea, rtld_errno, %a0, %a0); \
  78. neg.l %d0; \
  79. move.l %d0, (%a0); \
  80. move.l &-1, %d0; \
  81. /* Copy return value to %a0 for syscalls that are declared to return \
  82. a pointer (e.g., mmap). */ \
  83. move.l %d0, %a0; \
  84. rts;
  85. # elif defined _LIBC_REENTRANT
  86. # if IS_IN (libc)
  87. # define SYSCALL_ERROR_ERRNO __libc_errno
  88. # else
  89. # define SYSCALL_ERROR_ERRNO errno
  90. # endif
  91. # define SYSCALL_ERROR_HANDLER \
  92. SYSCALL_ERROR_LABEL: \
  93. neg.l %d0; \
  94. move.l %d0, -(%sp); \
  95. cfi_adjust_cfa_offset (4); \
  96. jbsr __m68k_read_tp@PLTPC; \
  97. SYSCALL_ERROR_LOAD_GOT (%a1); \
  98. add.l (SYSCALL_ERROR_ERRNO@TLSIE, %a1), %a0; \
  99. move.l (%sp)+, (%a0); \
  100. cfi_adjust_cfa_offset (-4); \
  101. move.l &-1, %d0; \
  102. /* Copy return value to %a0 for syscalls that are declared to return \
  103. a pointer (e.g., mmap). */ \
  104. move.l %d0, %a0; \
  105. rts;
  106. # else /* !_LIBC_REENTRANT */
  107. /* Store (- %d0) into errno through the GOT. */
  108. # define SYSCALL_ERROR_HANDLER \
  109. SYSCALL_ERROR_LABEL: \
  110. move.l (errno@GOTPC, %pc), %a0; \
  111. neg.l %d0; \
  112. move.l %d0, (%a0); \
  113. move.l &-1, %d0; \
  114. /* Copy return value to %a0 for syscalls that are declared to return \
  115. a pointer (e.g., mmap). */ \
  116. move.l %d0, %a0; \
  117. rts;
  118. # endif /* _LIBC_REENTRANT */
  119. #else
  120. # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
  121. #endif /* PIC */
  122. /* Linux takes system call arguments in registers:
  123. syscall number %d0 call-clobbered
  124. arg 1 %d1 call-clobbered
  125. arg 2 %d2 call-saved
  126. arg 3 %d3 call-saved
  127. arg 4 %d4 call-saved
  128. arg 5 %d5 call-saved
  129. arg 6 %a0 call-clobbered
  130. The stack layout upon entering the function is:
  131. 24(%sp) Arg# 6
  132. 20(%sp) Arg# 5
  133. 16(%sp) Arg# 4
  134. 12(%sp) Arg# 3
  135. 8(%sp) Arg# 2
  136. 4(%sp) Arg# 1
  137. (%sp) Return address
  138. (Of course a function with say 3 arguments does not have entries for
  139. arguments 4 and 5.)
  140. Separate move's are faster than movem, but need more space. Since
  141. speed is more important, we don't use movem. Since %a0 and %a1 are
  142. scratch registers, we can use them for saving as well. */
  143. #define DO_CALL(syscall_name, args) \
  144. move.l &SYS_ify(syscall_name), %d0; \
  145. DOARGS_##args \
  146. trap &0; \
  147. UNDOARGS_##args
  148. #define DOARGS_0 /* No arguments to frob. */
  149. #define UNDOARGS_0 /* No arguments to unfrob. */
  150. #define _DOARGS_0(n) /* No arguments to frob. */
  151. #define DOARGS_1 _DOARGS_1 (4)
  152. #define _DOARGS_1(n) move.l n(%sp), %d1; _DOARGS_0 (n)
  153. #define UNDOARGS_1 UNDOARGS_0
  154. #define DOARGS_2 _DOARGS_2 (8)
  155. #define _DOARGS_2(n) move.l %d2, %a0; cfi_register (%d2, %a0); \
  156. move.l n(%sp), %d2; _DOARGS_1 (n-4)
  157. #define UNDOARGS_2 UNDOARGS_1; move.l %a0, %d2; cfi_restore (%d2)
  158. #define DOARGS_3 _DOARGS_3 (12)
  159. #define _DOARGS_3(n) move.l %d3, %a1; cfi_register (%d3, %a1); \
  160. move.l n(%sp), %d3; _DOARGS_2 (n-4)
  161. #define UNDOARGS_3 UNDOARGS_2; move.l %a1, %d3; cfi_restore (%d3)
  162. #define DOARGS_4 _DOARGS_4 (16)
  163. #define _DOARGS_4(n) move.l %d4, -(%sp); \
  164. cfi_adjust_cfa_offset (4); cfi_rel_offset (%d4, 0); \
  165. move.l n+4(%sp), %d4; _DOARGS_3 (n)
  166. #define UNDOARGS_4 UNDOARGS_3; move.l (%sp)+, %d4; \
  167. cfi_adjust_cfa_offset (-4); cfi_restore (%d4)
  168. #define DOARGS_5 _DOARGS_5 (20)
  169. #define _DOARGS_5(n) move.l %d5, -(%sp); \
  170. cfi_adjust_cfa_offset (4); cfi_rel_offset (%d5, 0); \
  171. move.l n+4(%sp), %d5; _DOARGS_4 (n)
  172. #define UNDOARGS_5 UNDOARGS_4; move.l (%sp)+, %d5; \
  173. cfi_adjust_cfa_offset (-4); cfi_restore (%d5)
  174. #define DOARGS_6 _DOARGS_6 (24)
  175. #define _DOARGS_6(n) _DOARGS_5 (n-4); move.l %a0, -(%sp); \
  176. cfi_adjust_cfa_offset (4); \
  177. move.l n+12(%sp), %a0;
  178. #define UNDOARGS_6 move.l (%sp)+, %a0; cfi_adjust_cfa_offset (-4); \
  179. UNDOARGS_5
  180. #define ret rts
  181. #if 0 /* Not used by Linux */
  182. #define r0 %d0
  183. #define r1 %d1
  184. #define MOVE(x,y) movel x , y
  185. #endif
  186. #else /* not __ASSEMBLER__ */
  187. /* Define a macro which expands into the inline wrapper code for a system
  188. call. */
  189. #undef INLINE_SYSCALL
  190. #define INLINE_SYSCALL(name, nr, args...) \
  191. ({ unsigned int _sys_result = INTERNAL_SYSCALL (name, , nr, args); \
  192. if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_sys_result, ), 0))\
  193. { \
  194. __set_errno (INTERNAL_SYSCALL_ERRNO (_sys_result, )); \
  195. _sys_result = (unsigned int) -1; \
  196. } \
  197. (int) _sys_result; })
  198. #undef INTERNAL_SYSCALL_DECL
  199. #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
  200. /* Define a macro which expands inline into the wrapper code for a system
  201. call. This use is for internal calls that do not need to handle errors
  202. normally. It will never touch errno. This returns just what the kernel
  203. gave back. */
  204. #undef INTERNAL_SYSCALL
  205. #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
  206. ({ unsigned int _sys_result; \
  207. { \
  208. /* Load argument values in temporary variables
  209. to perform side effects like function calls
  210. before the call used registers are set. */ \
  211. LOAD_ARGS_##nr (args) \
  212. LOAD_REGS_##nr \
  213. register int _d0 asm ("%d0") = name; \
  214. asm volatile ("trap #0" \
  215. : "=d" (_d0) \
  216. : "0" (_d0) ASM_ARGS_##nr \
  217. : "memory"); \
  218. _sys_result = _d0; \
  219. } \
  220. (int) _sys_result; })
  221. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  222. INTERNAL_SYSCALL_NCS (__NR_##name, err, nr, ##args)
  223. #undef INTERNAL_SYSCALL_ERROR_P
  224. #define INTERNAL_SYSCALL_ERROR_P(val, err) \
  225. ((unsigned int) (val) >= -4095U)
  226. #undef INTERNAL_SYSCALL_ERRNO
  227. #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
  228. #define LOAD_ARGS_0()
  229. #define LOAD_REGS_0
  230. #define ASM_ARGS_0
  231. #define LOAD_ARGS_1(a1) \
  232. LOAD_ARGS_0 () \
  233. int __arg1 = (int) (a1);
  234. #define LOAD_REGS_1 \
  235. register int _d1 asm ("d1") = __arg1; \
  236. LOAD_REGS_0
  237. #define ASM_ARGS_1 ASM_ARGS_0, "d" (_d1)
  238. #define LOAD_ARGS_2(a1, a2) \
  239. LOAD_ARGS_1 (a1) \
  240. int __arg2 = (int) (a2);
  241. #define LOAD_REGS_2 \
  242. register int _d2 asm ("d2") = __arg2; \
  243. LOAD_REGS_1
  244. #define ASM_ARGS_2 ASM_ARGS_1, "d" (_d2)
  245. #define LOAD_ARGS_3(a1, a2, a3) \
  246. LOAD_ARGS_2 (a1, a2) \
  247. int __arg3 = (int) (a3);
  248. #define LOAD_REGS_3 \
  249. register int _d3 asm ("d3") = __arg3; \
  250. LOAD_REGS_2
  251. #define ASM_ARGS_3 ASM_ARGS_2, "d" (_d3)
  252. #define LOAD_ARGS_4(a1, a2, a3, a4) \
  253. LOAD_ARGS_3 (a1, a2, a3) \
  254. int __arg4 = (int) (a4);
  255. #define LOAD_REGS_4 \
  256. register int _d4 asm ("d4") = __arg4; \
  257. LOAD_REGS_3
  258. #define ASM_ARGS_4 ASM_ARGS_3, "d" (_d4)
  259. #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
  260. LOAD_ARGS_4 (a1, a2, a3, a4) \
  261. int __arg5 = (int) (a5);
  262. #define LOAD_REGS_5 \
  263. register int _d5 asm ("d5") = __arg5; \
  264. LOAD_REGS_4
  265. #define ASM_ARGS_5 ASM_ARGS_4, "d" (_d5)
  266. #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
  267. LOAD_ARGS_5 (a1, a2, a3, a4, a5) \
  268. int __arg6 = (int) (a6);
  269. #define LOAD_REGS_6 \
  270. register int _a0 asm ("a0") = __arg6; \
  271. LOAD_REGS_5
  272. #define ASM_ARGS_6 ASM_ARGS_5, "a" (_a0)
  273. #endif /* not __ASSEMBLER__ */
  274. /* Pointer mangling is not yet supported for M68K. */
  275. #define PTR_MANGLE(var) (void) (var)
  276. #define PTR_DEMANGLE(var) (void) (var)
  277. #if defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO
  278. /* M68K needs system-supplied DSO to access TLS helpers
  279. even when statically linked. */
  280. # define NEED_STATIC_SYSINFO_DSO 1
  281. #endif