syscalls.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. # undef INTERNAL_SYSCALL_DECL
  127. # define INTERNAL_SYSCALL_DECL(err) long int err __attribute__((unused))
  128. # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
  129. (__extension__ \
  130. ({ \
  131. register long int r0 __asm__ ("r0"); \
  132. register long int r3 __asm__ ("r3"); \
  133. register long int r4 __asm__ ("r4"); \
  134. register long int r5 __asm__ ("r5"); \
  135. register long int r6 __asm__ ("r6"); \
  136. register long int r7 __asm__ ("r7"); \
  137. register long int r8 __asm__ ("r8"); \
  138. register long int r9 __asm__ ("r9"); \
  139. register long int r10 __asm__ ("r10"); \
  140. register long int r11 __asm__ ("r11"); \
  141. register long int r12 __asm__ ("r12"); \
  142. LOAD_ARGS_##nr(name, args); \
  143. __asm__ __volatile__ \
  144. ("sc \n\t" \
  145. "mfcr %0" \
  146. : "=&r" (r0), \
  147. "=&r" (r3), "=&r" (r4), "=&r" (r5), "=&r" (r6), "=&r" (r7), \
  148. "=&r" (r8), "=&r" (r9), "=&r" (r10), "=&r" (r11), "=&r" (r12) \
  149. : ASM_INPUT_##nr \
  150. : "cr0", "ctr", "memory"); \
  151. err = r0; \
  152. (int) r3; \
  153. }) \
  154. )
  155. # define INTERNAL_SYSCALL_ERROR_P(val, err) \
  156. ((void) (val), unlikely ((err) & (1 << 28)))
  157. # define INTERNAL_SYSCALL_ERRNO(val, err) (val)
  158. extern void __illegally_sized_syscall_arg1(void);
  159. extern void __illegally_sized_syscall_arg2(void);
  160. extern void __illegally_sized_syscall_arg3(void);
  161. extern void __illegally_sized_syscall_arg4(void);
  162. extern void __illegally_sized_syscall_arg5(void);
  163. extern void __illegally_sized_syscall_arg6(void);
  164. # define LOAD_ARGS_0(name, dummy) \
  165. r0 = name
  166. # define LOAD_ARGS_1(name, __arg1) \
  167. long int arg1 = (long int) (__arg1); \
  168. LOAD_ARGS_0(name, 0); \
  169. if (__builtin_classify_type (__arg1) != 5 && sizeof (__arg1) > 4) \
  170. __illegally_sized_syscall_arg1 (); \
  171. r3 = arg1
  172. # define LOAD_ARGS_2(name, __arg1, __arg2) \
  173. long int arg2 = (long int) (__arg2); \
  174. LOAD_ARGS_1(name, __arg1); \
  175. if (__builtin_classify_type (__arg2) != 5 && sizeof (__arg2) > 4) \
  176. __illegally_sized_syscall_arg2 (); \
  177. r4 = arg2
  178. # define LOAD_ARGS_3(name, __arg1, __arg2, __arg3) \
  179. long int arg3 = (long int) (__arg3); \
  180. LOAD_ARGS_2(name, __arg1, __arg2); \
  181. if (__builtin_classify_type (__arg3) != 5 && sizeof (__arg3) > 4) \
  182. __illegally_sized_syscall_arg3 (); \
  183. r5 = arg3
  184. # define LOAD_ARGS_4(name, __arg1, __arg2, __arg3, __arg4) \
  185. long int arg4 = (long int) (__arg4); \
  186. LOAD_ARGS_3(name, __arg1, __arg2, __arg3); \
  187. if (__builtin_classify_type (__arg4) != 5 && sizeof (__arg4) > 4) \
  188. __illegally_sized_syscall_arg4 (); \
  189. r6 = arg4
  190. # define LOAD_ARGS_5(name, __arg1, __arg2, __arg3, __arg4, __arg5) \
  191. long int arg5 = (long int) (__arg5); \
  192. LOAD_ARGS_4(name, __arg1, __arg2, __arg3, __arg4); \
  193. if (__builtin_classify_type (__arg5) != 5 && sizeof (__arg5) > 4) \
  194. __illegally_sized_syscall_arg5 (); \
  195. r7 = arg5
  196. # define LOAD_ARGS_6(name, __arg1, __arg2, __arg3, __arg4, __arg5, __arg6) \
  197. long int arg6 = (long int) (__arg6); \
  198. LOAD_ARGS_5(name, __arg1, __arg2, __arg3, __arg4, __arg5); \
  199. if (__builtin_classify_type (__arg6) != 5 && sizeof (__arg6) > 4) \
  200. __illegally_sized_syscall_arg6 (); \
  201. r8 = arg6
  202. # define ASM_INPUT_0 "0" (r0)
  203. # define ASM_INPUT_1 ASM_INPUT_0, "1" (r3)
  204. # define ASM_INPUT_2 ASM_INPUT_1, "2" (r4)
  205. # define ASM_INPUT_3 ASM_INPUT_2, "3" (r5)
  206. # define ASM_INPUT_4 ASM_INPUT_3, "4" (r6)
  207. # define ASM_INPUT_5 ASM_INPUT_4, "5" (r7)
  208. # define ASM_INPUT_6 ASM_INPUT_5, "6" (r8)
  209. #endif /* __ASSEMBLER__ */
  210. #endif /* _BITS_SYSCALLS_H */