syscalls.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. LOADARGS_##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 INLINE_SYSCALL(name, nr, args...) \
  121. ({ \
  122. INTERNAL_SYSCALL_DECL (sc_err); \
  123. long int sc_ret = INTERNAL_SYSCALL (name, sc_err, nr, args); \
  124. if (INTERNAL_SYSCALL_ERROR_P (sc_ret, sc_err)) \
  125. { \
  126. __set_errno (INTERNAL_SYSCALL_ERRNO (sc_ret, sc_err)); \
  127. sc_ret = -1L; \
  128. } \
  129. sc_ret; \
  130. })
  131. /* Define a macro which expands inline into the wrapper code for a system
  132. call. This use is for internal calls that do not need to handle errors
  133. normally. It will never touch errno.
  134. On powerpc a system call basically clobbers the same registers like a
  135. function call, with the exception of LR (which is needed for the
  136. "sc; bnslr+" sequence) and CR (where only CR0.SO is clobbered to signal
  137. an error return status). */
  138. # define INTERNAL_SYSCALL_DECL(err) long int err
  139. # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
  140. ({ \
  141. register long int r0 __asm__ ("r0"); \
  142. register long int r3 __asm__ ("r3"); \
  143. register long int r4 __asm__ ("r4"); \
  144. register long int r5 __asm__ ("r5"); \
  145. register long int r6 __asm__ ("r6"); \
  146. register long int r7 __asm__ ("r7"); \
  147. register long int r8 __asm__ ("r8"); \
  148. register long int r9 __asm__ ("r9"); \
  149. register long int r10 __asm__ ("r10"); \
  150. register long int r11 __asm__ ("r11"); \
  151. register long int r12 __asm__ ("r12"); \
  152. LOADARGS_##nr(name, args); \
  153. __asm__ __volatile__ \
  154. ("sc \n\t" \
  155. "mfcr %0" \
  156. : "=&r" (r0), \
  157. "=&r" (r3), "=&r" (r4), "=&r" (r5), "=&r" (r6), "=&r" (r7), \
  158. "=&r" (r8), "=&r" (r9), "=&r" (r10), "=&r" (r11), "=&r" (r12) \
  159. : ASM_INPUT_##nr \
  160. : "cr0", "ctr", "memory"); \
  161. err = r0; \
  162. (int) r3; \
  163. })
  164. # define INTERNAL_SYSCALL(name, err, nr, args...) \
  165. INTERNAL_SYSCALL_NCS (__NR_##name, err, nr, ##args)
  166. # define INTERNAL_SYSCALL_ERROR_P(val, err) \
  167. ((void) (val), __builtin_expect ((err) & (1 << 28), 0))
  168. # define INTERNAL_SYSCALL_ERRNO(val, err) (val)
  169. extern void __illegally_sized_syscall_arg1(void);
  170. extern void __illegally_sized_syscall_arg2(void);
  171. extern void __illegally_sized_syscall_arg3(void);
  172. extern void __illegally_sized_syscall_arg4(void);
  173. extern void __illegally_sized_syscall_arg5(void);
  174. extern void __illegally_sized_syscall_arg6(void);
  175. # define LOADARGS_0(name, dummy) \
  176. r0 = name
  177. # define LOADARGS_1(name, __arg1) \
  178. long int arg1 = (long int) (__arg1); \
  179. LOADARGS_0(name, 0); \
  180. if (__builtin_classify_type (__arg1) != 5 && sizeof (__arg1) > 4) \
  181. __illegally_sized_syscall_arg1 (); \
  182. r3 = arg1
  183. # define LOADARGS_2(name, __arg1, __arg2) \
  184. long int arg2 = (long int) (__arg2); \
  185. LOADARGS_1(name, __arg1); \
  186. if (__builtin_classify_type (__arg2) != 5 && sizeof (__arg2) > 4) \
  187. __illegally_sized_syscall_arg2 (); \
  188. r4 = arg2
  189. # define LOADARGS_3(name, __arg1, __arg2, __arg3) \
  190. long int arg3 = (long int) (__arg3); \
  191. LOADARGS_2(name, __arg1, __arg2); \
  192. if (__builtin_classify_type (__arg3) != 5 && sizeof (__arg3) > 4) \
  193. __illegally_sized_syscall_arg3 (); \
  194. r5 = arg3
  195. # define LOADARGS_4(name, __arg1, __arg2, __arg3, __arg4) \
  196. long int arg4 = (long int) (__arg4); \
  197. LOADARGS_3(name, __arg1, __arg2, __arg3); \
  198. if (__builtin_classify_type (__arg4) != 5 && sizeof (__arg4) > 4) \
  199. __illegally_sized_syscall_arg4 (); \
  200. r6 = arg4
  201. # define LOADARGS_5(name, __arg1, __arg2, __arg3, __arg4, __arg5) \
  202. long int arg5 = (long int) (__arg5); \
  203. LOADARGS_4(name, __arg1, __arg2, __arg3, __arg4); \
  204. if (__builtin_classify_type (__arg5) != 5 && sizeof (__arg5) > 4) \
  205. __illegally_sized_syscall_arg5 (); \
  206. r7 = arg5
  207. # define LOADARGS_6(name, __arg1, __arg2, __arg3, __arg4, __arg5, __arg6) \
  208. long int arg6 = (long int) (__arg6); \
  209. LOADARGS_5(name, __arg1, __arg2, __arg3, __arg4, __arg5); \
  210. if (__builtin_classify_type (__arg6) != 5 && sizeof (__arg6) > 4) \
  211. __illegally_sized_syscall_arg6 (); \
  212. r8 = arg6
  213. # define ASM_INPUT_0 "0" (r0)
  214. # define ASM_INPUT_1 ASM_INPUT_0, "1" (r3)
  215. # define ASM_INPUT_2 ASM_INPUT_1, "2" (r4)
  216. # define ASM_INPUT_3 ASM_INPUT_2, "3" (r5)
  217. # define ASM_INPUT_4 ASM_INPUT_3, "4" (r6)
  218. # define ASM_INPUT_5 ASM_INPUT_4, "5" (r7)
  219. # define ASM_INPUT_6 ASM_INPUT_5, "6" (r8)
  220. #endif /* __ASSEMBLER__ */
  221. /* Pointer mangling support. */
  222. #if defined NOT_IN_libc && defined IS_IN_rtld
  223. /* We cannot use the thread descriptor because in ld.so we use setjmp
  224. earlier than the descriptor is initialized. */
  225. #else
  226. # ifdef __ASSEMBLER__
  227. # define PTR_MANGLE(reg, tmpreg) \
  228. lwz tmpreg,POINTER_GUARD(r2); \
  229. xor reg,tmpreg,reg
  230. # define PTR_MANGLE2(reg, tmpreg) \
  231. xor reg,tmpreg,reg
  232. # define PTR_MANGLE3(destreg, reg, tmpreg) \
  233. lwz tmpreg,POINTER_GUARD(r2); \
  234. xor destreg,tmpreg,reg
  235. # define PTR_DEMANGLE(reg, tmpreg) PTR_MANGLE (reg, tmpreg)
  236. # define PTR_DEMANGLE2(reg, tmpreg) PTR_MANGLE2 (reg, tmpreg)
  237. # define PTR_DEMANGLE3(destreg, reg, tmpreg) PTR_MANGLE3 (destreg, reg, tmpreg)
  238. # else
  239. # define PTR_MANGLE(var) \
  240. (var) = (__typeof (var)) ((uintptr_t) (var) ^ THREAD_GET_POINTER_GUARD ())
  241. # define PTR_DEMANGLE(var) PTR_MANGLE (var)
  242. # endif
  243. #endif
  244. #endif /* _BITS_SYSCALLS_H */