syscalls.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* Copyright (C) 1992,1997-2003,2004,2005,2006 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. #ifndef _BITS_SYSCALLS_H
  15. #define _BITS_SYSCALLS_H
  16. #ifndef _SYSCALL_H
  17. # error "Never use <bits/syscalls.h> directly; include <sys/syscall.h> instead."
  18. #endif
  19. #ifndef __ASSEMBLER__
  20. # include <errno.h>
  21. # ifdef SHARED
  22. # define INLINE_VSYSCALL(name, nr, args...) \
  23. ({ \
  24. __label__ out; \
  25. __label__ iserr; \
  26. INTERNAL_SYSCALL_DECL (sc_err); \
  27. long int sc_ret; \
  28. \
  29. if (__vdso_##name != NULL) \
  30. { \
  31. sc_ret = INTERNAL_VSYSCALL_NCS (__vdso_##name, sc_err, nr, ##args); \
  32. if (!INTERNAL_SYSCALL_ERROR_P (sc_ret, sc_err)) \
  33. goto out; \
  34. if (INTERNAL_SYSCALL_ERRNO (sc_ret, sc_err) != ENOSYS) \
  35. goto iserr; \
  36. } \
  37. \
  38. sc_ret = INTERNAL_SYSCALL (name, sc_err, nr, ##args); \
  39. if (INTERNAL_SYSCALL_ERROR_P (sc_ret, sc_err)) \
  40. { \
  41. iserr: \
  42. __set_errno (INTERNAL_SYSCALL_ERRNO (sc_ret, sc_err)); \
  43. sc_ret = -1L; \
  44. } \
  45. out: \
  46. sc_ret; \
  47. })
  48. # else
  49. # define INLINE_VSYSCALL(name, nr, args...) \
  50. INLINE_SYSCALL (name, nr, ##args)
  51. # endif
  52. # ifdef SHARED
  53. # define INTERNAL_VSYSCALL(name, err, nr, args...) \
  54. ({ \
  55. __label__ out; \
  56. long int v_ret; \
  57. \
  58. if (__vdso_##name != NULL) \
  59. { \
  60. v_ret = INTERNAL_VSYSCALL_NCS (__vdso_##name, err, nr, ##args); \
  61. if (!INTERNAL_SYSCALL_ERROR_P (v_ret, err) \
  62. || INTERNAL_SYSCALL_ERRNO (v_ret, err) != ENOSYS) \
  63. goto out; \
  64. } \
  65. v_ret = INTERNAL_SYSCALL (name, err, nr, ##args); \
  66. out: \
  67. v_ret; \
  68. })
  69. # else
  70. # define INTERNAL_VSYSCALL(name, err, nr, args...) \
  71. INTERNAL_SYSCALL (name, err, nr, ##args)
  72. # endif
  73. # define INTERNAL_VSYSCALL_NO_SYSCALL_FALLBACK(name, err, nr, args...) \
  74. ({ \
  75. long int sc_ret = ENOSYS; \
  76. \
  77. if (__vdso_##name != NULL) \
  78. sc_ret = INTERNAL_VSYSCALL_NCS (__vdso_##name, err, nr, ##args); \
  79. else \
  80. err = 1 << 28; \
  81. sc_ret; \
  82. })
  83. /* List of system calls which are supported as vsyscalls. */
  84. # define HAVE_CLOCK_GETRES_VSYSCALL 1
  85. # define HAVE_CLOCK_GETTIME_VSYSCALL 1
  86. /* Define a macro which expands inline into the wrapper code for a VDSO
  87. call. This use is for internal calls that do not need to handle errors
  88. normally. It will never touch errno.
  89. On powerpc a system call basically clobbers the same registers like a
  90. function call, with the exception of LR (which is needed for the
  91. "sc; bnslr+" sequence) and CR (where only CR0.SO is clobbered to signal
  92. an error return status). */
  93. # define INTERNAL_VSYSCALL_NCS(funcptr, err, nr, args...) \
  94. ({ \
  95. register void *r0 __asm__ ("r0"); \
  96. register long int r3 __asm__ ("r3"); \
  97. register long int r4 __asm__ ("r4"); \
  98. register long int r5 __asm__ ("r5"); \
  99. register long int r6 __asm__ ("r6"); \
  100. register long int r7 __asm__ ("r7"); \
  101. register long int r8 __asm__ ("r8"); \
  102. register long int r9 __asm__ ("r9"); \
  103. register long int r10 __asm__ ("r10"); \
  104. register long int r11 __asm__ ("r11"); \
  105. register long int r12 __asm__ ("r12"); \
  106. LOAD_ARGS_##nr (funcptr, args); \
  107. __asm__ __volatile__ \
  108. ("mtctr %0\n\t" \
  109. "bctrl\n\t" \
  110. "mfcr %0" \
  111. : "=&r" (r0), \
  112. "=&r" (r3), "=&r" (r4), "=&r" (r5), "=&r" (r6), "=&r" (r7), \
  113. "=&r" (r8), "=&r" (r9), "=&r" (r10), "=&r" (r11), "=&r" (r12) \
  114. : ASM_INPUT_##nr \
  115. : "cr0", "ctr", "lr", "memory"); \
  116. err = (long int) r0; \
  117. (int) r3; \
  118. })
  119. /* Define a macro which expands inline into the wrapper code for a system
  120. call. This use is for internal calls that do not need to handle errors
  121. normally. It will never touch errno.
  122. On powerpc a system call basically clobbers the same registers like a
  123. function call, with the exception of LR (which is needed for the
  124. "sc; bnslr+" sequence) and CR (where only CR0.SO is clobbered to signal
  125. an error return status). */
  126. # define INTERNAL_SYSCALL_DECL(err) long int err
  127. # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
  128. (__extension__ \
  129. ({ \
  130. register long int r0 __asm__ ("r0"); \
  131. register long int r3 __asm__ ("r3"); \
  132. register long int r4 __asm__ ("r4"); \
  133. register long int r5 __asm__ ("r5"); \
  134. register long int r6 __asm__ ("r6"); \
  135. register long int r7 __asm__ ("r7"); \
  136. register long int r8 __asm__ ("r8"); \
  137. register long int r9 __asm__ ("r9"); \
  138. register long int r10 __asm__ ("r10"); \
  139. register long int r11 __asm__ ("r11"); \
  140. register long int r12 __asm__ ("r12"); \
  141. LOAD_ARGS_##nr(name, args); \
  142. __asm__ __volatile__ \
  143. ("sc \n\t" \
  144. "mfcr %0" \
  145. : "=&r" (r0), \
  146. "=&r" (r3), "=&r" (r4), "=&r" (r5), "=&r" (r6), "=&r" (r7), \
  147. "=&r" (r8), "=&r" (r9), "=&r" (r10), "=&r" (r11), "=&r" (r12) \
  148. : ASM_INPUT_##nr \
  149. : "cr0", "ctr", "memory"); \
  150. err = r0; \
  151. (int) r3; \
  152. }) \
  153. )
  154. # define INTERNAL_SYSCALL_ERROR_P(val, err) \
  155. ((void) (val), unlikely ((err) & (1 << 28)))
  156. # define INTERNAL_SYSCALL_ERRNO(val, err) (val)
  157. extern void __illegally_sized_syscall_arg1(void);
  158. extern void __illegally_sized_syscall_arg2(void);
  159. extern void __illegally_sized_syscall_arg3(void);
  160. extern void __illegally_sized_syscall_arg4(void);
  161. extern void __illegally_sized_syscall_arg5(void);
  162. extern void __illegally_sized_syscall_arg6(void);
  163. # define LOAD_ARGS_0(name, dummy) \
  164. r0 = name
  165. # define LOAD_ARGS_1(name, __arg1) \
  166. long int arg1 = (long int) (__arg1); \
  167. LOAD_ARGS_0(name, 0); \
  168. if (__builtin_classify_type (__arg1) != 5 && sizeof (__arg1) > 4) \
  169. __illegally_sized_syscall_arg1 (); \
  170. r3 = arg1
  171. # define LOAD_ARGS_2(name, __arg1, __arg2) \
  172. long int arg2 = (long int) (__arg2); \
  173. LOAD_ARGS_1(name, __arg1); \
  174. if (__builtin_classify_type (__arg2) != 5 && sizeof (__arg2) > 4) \
  175. __illegally_sized_syscall_arg2 (); \
  176. r4 = arg2
  177. # define LOAD_ARGS_3(name, __arg1, __arg2, __arg3) \
  178. long int arg3 = (long int) (__arg3); \
  179. LOAD_ARGS_2(name, __arg1, __arg2); \
  180. if (__builtin_classify_type (__arg3) != 5 && sizeof (__arg3) > 4) \
  181. __illegally_sized_syscall_arg3 (); \
  182. r5 = arg3
  183. # define LOAD_ARGS_4(name, __arg1, __arg2, __arg3, __arg4) \
  184. long int arg4 = (long int) (__arg4); \
  185. LOAD_ARGS_3(name, __arg1, __arg2, __arg3); \
  186. if (__builtin_classify_type (__arg4) != 5 && sizeof (__arg4) > 4) \
  187. __illegally_sized_syscall_arg4 (); \
  188. r6 = arg4
  189. # define LOAD_ARGS_5(name, __arg1, __arg2, __arg3, __arg4, __arg5) \
  190. long int arg5 = (long int) (__arg5); \
  191. LOAD_ARGS_4(name, __arg1, __arg2, __arg3, __arg4); \
  192. if (__builtin_classify_type (__arg5) != 5 && sizeof (__arg5) > 4) \
  193. __illegally_sized_syscall_arg5 (); \
  194. r7 = arg5
  195. # define LOAD_ARGS_6(name, __arg1, __arg2, __arg3, __arg4, __arg5, __arg6) \
  196. long int arg6 = (long int) (__arg6); \
  197. LOAD_ARGS_5(name, __arg1, __arg2, __arg3, __arg4, __arg5); \
  198. if (__builtin_classify_type (__arg6) != 5 && sizeof (__arg6) > 4) \
  199. __illegally_sized_syscall_arg6 (); \
  200. r8 = arg6
  201. # define ASM_INPUT_0 "0" (r0)
  202. # define ASM_INPUT_1 ASM_INPUT_0, "1" (r3)
  203. # define ASM_INPUT_2 ASM_INPUT_1, "2" (r4)
  204. # define ASM_INPUT_3 ASM_INPUT_2, "3" (r5)
  205. # define ASM_INPUT_4 ASM_INPUT_3, "4" (r6)
  206. # define ASM_INPUT_5 ASM_INPUT_4, "5" (r7)
  207. # define ASM_INPUT_6 ASM_INPUT_5, "6" (r8)
  208. #endif /* __ASSEMBLER__ */
  209. /* Pointer mangling support. */
  210. #if defined NOT_IN_libc && defined IS_IN_rtld
  211. /* We cannot use the thread descriptor because in ld.so we use setjmp
  212. earlier than the descriptor is initialized. */
  213. #else
  214. # ifdef __ASSEMBLER__
  215. # define PTR_MANGLE(reg, tmpreg) \
  216. lwz tmpreg,POINTER_GUARD(r2); \
  217. xor reg,tmpreg,reg
  218. # define PTR_MANGLE2(reg, tmpreg) \
  219. xor reg,tmpreg,reg
  220. # define PTR_MANGLE3(destreg, reg, tmpreg) \
  221. lwz tmpreg,POINTER_GUARD(r2); \
  222. xor destreg,tmpreg,reg
  223. # define PTR_DEMANGLE(reg, tmpreg) PTR_MANGLE (reg, tmpreg)
  224. # define PTR_DEMANGLE2(reg, tmpreg) PTR_MANGLE2 (reg, tmpreg)
  225. # define PTR_DEMANGLE3(destreg, reg, tmpreg) PTR_MANGLE3 (destreg, reg, tmpreg)
  226. # else
  227. # define PTR_MANGLE(var) \
  228. (var) = (__typeof (var)) ((uintptr_t) (var) ^ THREAD_GET_POINTER_GUARD ())
  229. # define PTR_DEMANGLE(var) PTR_MANGLE (var)
  230. # endif
  231. #endif
  232. #endif /* _BITS_SYSCALLS_H */