syscalls.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. sc_ret; \
  80. })
  81. /* List of system calls which are supported as vsyscalls. */
  82. # define HAVE_CLOCK_GETRES_VSYSCALL 1
  83. # define HAVE_CLOCK_GETTIME_VSYSCALL 1
  84. /* Define a macro which expands inline into the wrapper code for a VDSO
  85. call. This use is for internal calls that do not need to handle errors
  86. normally. It will never touch errno.
  87. On powerpc a system call basically clobbers the same registers like a
  88. function call, with the exception of LR (which is needed for the
  89. "sc; bnslr+" sequence) and CR (where only CR0.SO is clobbered to signal
  90. an error return status). */
  91. # define INTERNAL_VSYSCALL_NCS(funcptr, err, nr, args...) \
  92. ({ \
  93. register void *r0 __asm__ ("r0"); \
  94. register long int r3 __asm__ ("r3"); \
  95. register long int r4 __asm__ ("r4"); \
  96. register long int r5 __asm__ ("r5"); \
  97. register long int r6 __asm__ ("r6"); \
  98. register long int r7 __asm__ ("r7"); \
  99. register long int r8 __asm__ ("r8"); \
  100. register long int r9 __asm__ ("r9"); \
  101. register long int r10 __asm__ ("r10"); \
  102. register long int r11 __asm__ ("r11"); \
  103. register long int r12 __asm__ ("r12"); \
  104. LOAD_ARGS_##nr (funcptr, args); \
  105. __asm__ __volatile__ \
  106. ("mtctr %0\n\t" \
  107. "bctrl\n\t" \
  108. "mfcr %0" \
  109. : "=&r" (r0), \
  110. "=&r" (r3), "=&r" (r4), "=&r" (r5), "=&r" (r6), "=&r" (r7), \
  111. "=&r" (r8), "=&r" (r9), "=&r" (r10), "=&r" (r11), "=&r" (r12) \
  112. : ASM_INPUT_##nr \
  113. : "cr0", "ctr", "lr", "memory"); \
  114. ((long int) r0) & (1 << 28) ? -r3 : r3; \
  115. })
  116. /* Define a macro which expands inline into the wrapper code for a system
  117. call. This use is for internal calls that do not need to handle errors
  118. normally. It will never touch errno.
  119. On powerpc a system call basically clobbers the same registers like a
  120. function call, with the exception of LR (which is needed for the
  121. "sc; bnslr+" sequence) and CR (where only CR0.SO is clobbered to signal
  122. an error return status). */
  123. # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
  124. (__extension__ \
  125. ({ \
  126. register long int r0 __asm__ ("r0"); \
  127. register long int r3 __asm__ ("r3"); \
  128. register long int r4 __asm__ ("r4"); \
  129. register long int r5 __asm__ ("r5"); \
  130. register long int r6 __asm__ ("r6"); \
  131. register long int r7 __asm__ ("r7"); \
  132. register long int r8 __asm__ ("r8"); \
  133. register long int r9 __asm__ ("r9"); \
  134. register long int r10 __asm__ ("r10"); \
  135. register long int r11 __asm__ ("r11"); \
  136. register long int r12 __asm__ ("r12"); \
  137. LOAD_ARGS_##nr(name, args); \
  138. __asm__ __volatile__ \
  139. ("sc \n\t" \
  140. "mfcr %0" \
  141. : "=&r" (r0), \
  142. "=&r" (r3), "=&r" (r4), "=&r" (r5), "=&r" (r6), "=&r" (r7), \
  143. "=&r" (r8), "=&r" (r9), "=&r" (r10), "=&r" (r11), "=&r" (r12) \
  144. : ASM_INPUT_##nr \
  145. : "cr0", "ctr", "memory"); \
  146. /* CR0.SO signals the error; fold it into a negative return value. */ \
  147. r0 & (1 << 28) ? -r3 : r3; \
  148. }) \
  149. )
  150. extern void __illegally_sized_syscall_arg1(void);
  151. extern void __illegally_sized_syscall_arg2(void);
  152. extern void __illegally_sized_syscall_arg3(void);
  153. extern void __illegally_sized_syscall_arg4(void);
  154. extern void __illegally_sized_syscall_arg5(void);
  155. extern void __illegally_sized_syscall_arg6(void);
  156. # define LOAD_ARGS_0(name, dummy) \
  157. r0 = name
  158. # define LOAD_ARGS_1(name, __arg1) \
  159. long int arg1 = (long int) (__arg1); \
  160. LOAD_ARGS_0(name, 0); \
  161. if (__builtin_classify_type (__arg1) != 5 && sizeof (__arg1) > 4) \
  162. __illegally_sized_syscall_arg1 (); \
  163. r3 = arg1
  164. # define LOAD_ARGS_2(name, __arg1, __arg2) \
  165. long int arg2 = (long int) (__arg2); \
  166. LOAD_ARGS_1(name, __arg1); \
  167. if (__builtin_classify_type (__arg2) != 5 && sizeof (__arg2) > 4) \
  168. __illegally_sized_syscall_arg2 (); \
  169. r4 = arg2
  170. # define LOAD_ARGS_3(name, __arg1, __arg2, __arg3) \
  171. long int arg3 = (long int) (__arg3); \
  172. LOAD_ARGS_2(name, __arg1, __arg2); \
  173. if (__builtin_classify_type (__arg3) != 5 && sizeof (__arg3) > 4) \
  174. __illegally_sized_syscall_arg3 (); \
  175. r5 = arg3
  176. # define LOAD_ARGS_4(name, __arg1, __arg2, __arg3, __arg4) \
  177. long int arg4 = (long int) (__arg4); \
  178. LOAD_ARGS_3(name, __arg1, __arg2, __arg3); \
  179. if (__builtin_classify_type (__arg4) != 5 && sizeof (__arg4) > 4) \
  180. __illegally_sized_syscall_arg4 (); \
  181. r6 = arg4
  182. # define LOAD_ARGS_5(name, __arg1, __arg2, __arg3, __arg4, __arg5) \
  183. long int arg5 = (long int) (__arg5); \
  184. LOAD_ARGS_4(name, __arg1, __arg2, __arg3, __arg4); \
  185. if (__builtin_classify_type (__arg5) != 5 && sizeof (__arg5) > 4) \
  186. __illegally_sized_syscall_arg5 (); \
  187. r7 = arg5
  188. # define LOAD_ARGS_6(name, __arg1, __arg2, __arg3, __arg4, __arg5, __arg6) \
  189. long int arg6 = (long int) (__arg6); \
  190. LOAD_ARGS_5(name, __arg1, __arg2, __arg3, __arg4, __arg5); \
  191. if (__builtin_classify_type (__arg6) != 5 && sizeof (__arg6) > 4) \
  192. __illegally_sized_syscall_arg6 (); \
  193. r8 = arg6
  194. # define ASM_INPUT_0 "0" (r0)
  195. # define ASM_INPUT_1 ASM_INPUT_0, "1" (r3)
  196. # define ASM_INPUT_2 ASM_INPUT_1, "2" (r4)
  197. # define ASM_INPUT_3 ASM_INPUT_2, "3" (r5)
  198. # define ASM_INPUT_4 ASM_INPUT_3, "4" (r6)
  199. # define ASM_INPUT_5 ASM_INPUT_4, "5" (r7)
  200. # define ASM_INPUT_6 ASM_INPUT_5, "6" (r8)
  201. #endif /* __ASSEMBLER__ */
  202. #endif /* _BITS_SYSCALLS_H */