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