syscall_wrapper.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * syscall_wrapper.h - x86 specific wrappers to syscall definitions
  4. */
  5. #ifndef _ASM_X86_SYSCALL_WRAPPER_H
  6. #define _ASM_X86_SYSCALL_WRAPPER_H
  7. #include <asm/ptrace.h>
  8. extern long __x64_sys_ni_syscall(const struct pt_regs *regs);
  9. extern long __ia32_sys_ni_syscall(const struct pt_regs *regs);
  10. /*
  11. * Instead of the generic __SYSCALL_DEFINEx() definition, the x86 version takes
  12. * struct pt_regs *regs as the only argument of the syscall stub(s) named as:
  13. * __x64_sys_*() - 64-bit native syscall
  14. * __ia32_sys_*() - 32-bit native syscall or common compat syscall
  15. * __ia32_compat_sys_*() - 32-bit compat syscall
  16. * __x64_compat_sys_*() - 64-bit X32 compat syscall
  17. *
  18. * The registers are decoded according to the ABI:
  19. * 64-bit: RDI, RSI, RDX, R10, R8, R9
  20. * 32-bit: EBX, ECX, EDX, ESI, EDI, EBP
  21. *
  22. * The stub then passes the decoded arguments to the __se_sys_*() wrapper to
  23. * perform sign-extension (omitted for zero-argument syscalls). Finally the
  24. * arguments are passed to the __do_sys_*() function which is the actual
  25. * syscall. These wrappers are marked as inline so the compiler can optimize
  26. * the functions where appropriate.
  27. *
  28. * Example assembly (slightly re-ordered for better readability):
  29. *
  30. * <__x64_sys_recv>: <-- syscall with 4 parameters
  31. * callq <__fentry__>
  32. *
  33. * mov 0x70(%rdi),%rdi <-- decode regs->di
  34. * mov 0x68(%rdi),%rsi <-- decode regs->si
  35. * mov 0x60(%rdi),%rdx <-- decode regs->dx
  36. * mov 0x38(%rdi),%rcx <-- decode regs->r10
  37. *
  38. * xor %r9d,%r9d <-- clear %r9
  39. * xor %r8d,%r8d <-- clear %r8
  40. *
  41. * callq __sys_recvfrom <-- do the actual work in __sys_recvfrom()
  42. * which takes 6 arguments
  43. *
  44. * cltq <-- extend return value to 64-bit
  45. * retq <-- return
  46. *
  47. * This approach avoids leaking random user-provided register content down
  48. * the call chain.
  49. */
  50. /* Mapping of registers to parameters for syscalls on x86-64 and x32 */
  51. #define SC_X86_64_REGS_TO_ARGS(x, ...) \
  52. __MAP(x,__SC_ARGS \
  53. ,,regs->di,,regs->si,,regs->dx \
  54. ,,regs->r10,,regs->r8,,regs->r9) \
  55. /* Mapping of registers to parameters for syscalls on i386 */
  56. #define SC_IA32_REGS_TO_ARGS(x, ...) \
  57. __MAP(x,__SC_ARGS \
  58. ,,(unsigned int)regs->bx,,(unsigned int)regs->cx \
  59. ,,(unsigned int)regs->dx,,(unsigned int)regs->si \
  60. ,,(unsigned int)regs->di,,(unsigned int)regs->bp)
  61. #define __SYS_STUB0(abi, name) \
  62. long __##abi##_##name(const struct pt_regs *regs); \
  63. ALLOW_ERROR_INJECTION(__##abi##_##name, ERRNO); \
  64. long __##abi##_##name(const struct pt_regs *regs) \
  65. __alias(__do_##name);
  66. #define __SYS_STUBx(abi, name, ...) \
  67. long __##abi##_##name(const struct pt_regs *regs); \
  68. ALLOW_ERROR_INJECTION(__##abi##_##name, ERRNO); \
  69. long __##abi##_##name(const struct pt_regs *regs) \
  70. { \
  71. return __se_##name(__VA_ARGS__); \
  72. }
  73. #define __COND_SYSCALL(abi, name) \
  74. __weak long __##abi##_##name(const struct pt_regs *__unused); \
  75. __weak long __##abi##_##name(const struct pt_regs *__unused) \
  76. { \
  77. return sys_ni_syscall(); \
  78. }
  79. #define __SYS_NI(abi, name) \
  80. SYSCALL_ALIAS(__##abi##_##name, sys_ni_posix_timers);
  81. #ifdef CONFIG_X86_64
  82. #define __X64_SYS_STUB0(name) \
  83. __SYS_STUB0(x64, sys_##name)
  84. #define __X64_SYS_STUBx(x, name, ...) \
  85. __SYS_STUBx(x64, sys##name, \
  86. SC_X86_64_REGS_TO_ARGS(x, __VA_ARGS__))
  87. #define __X64_COND_SYSCALL(name) \
  88. __COND_SYSCALL(x64, sys_##name)
  89. #define __X64_SYS_NI(name) \
  90. __SYS_NI(x64, sys_##name)
  91. #else /* CONFIG_X86_64 */
  92. #define __X64_SYS_STUB0(name)
  93. #define __X64_SYS_STUBx(x, name, ...)
  94. #define __X64_COND_SYSCALL(name)
  95. #define __X64_SYS_NI(name)
  96. #endif /* CONFIG_X86_64 */
  97. #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
  98. #define __IA32_SYS_STUB0(name) \
  99. __SYS_STUB0(ia32, sys_##name)
  100. #define __IA32_SYS_STUBx(x, name, ...) \
  101. __SYS_STUBx(ia32, sys##name, \
  102. SC_IA32_REGS_TO_ARGS(x, __VA_ARGS__))
  103. #define __IA32_COND_SYSCALL(name) \
  104. __COND_SYSCALL(ia32, sys_##name)
  105. #define __IA32_SYS_NI(name) \
  106. __SYS_NI(ia32, sys_##name)
  107. #else /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */
  108. #define __IA32_SYS_STUB0(name)
  109. #define __IA32_SYS_STUBx(x, name, ...)
  110. #define __IA32_COND_SYSCALL(name)
  111. #define __IA32_SYS_NI(name)
  112. #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */
  113. #ifdef CONFIG_IA32_EMULATION
  114. /*
  115. * For IA32 emulation, we need to handle "compat" syscalls *and* create
  116. * additional wrappers (aptly named __ia32_sys_xyzzy) which decode the
  117. * ia32 regs in the proper order for shared or "common" syscalls. As some
  118. * syscalls may not be implemented, we need to expand COND_SYSCALL in
  119. * kernel/sys_ni.c and SYS_NI in kernel/time/posix-stubs.c to cover this
  120. * case as well.
  121. */
  122. #define __IA32_COMPAT_SYS_STUB0(name) \
  123. __SYS_STUB0(ia32, compat_sys_##name)
  124. #define __IA32_COMPAT_SYS_STUBx(x, name, ...) \
  125. __SYS_STUBx(ia32, compat_sys##name, \
  126. SC_IA32_REGS_TO_ARGS(x, __VA_ARGS__))
  127. #define __IA32_COMPAT_COND_SYSCALL(name) \
  128. __COND_SYSCALL(ia32, compat_sys_##name)
  129. #define __IA32_COMPAT_SYS_NI(name) \
  130. __SYS_NI(ia32, compat_sys_##name)
  131. #else /* CONFIG_IA32_EMULATION */
  132. #define __IA32_COMPAT_SYS_STUB0(name)
  133. #define __IA32_COMPAT_SYS_STUBx(x, name, ...)
  134. #define __IA32_COMPAT_COND_SYSCALL(name)
  135. #define __IA32_COMPAT_SYS_NI(name)
  136. #endif /* CONFIG_IA32_EMULATION */
  137. #ifdef CONFIG_X86_X32_ABI
  138. /*
  139. * For the x32 ABI, we need to create a stub for compat_sys_*() which is aware
  140. * of the x86-64-style parameter ordering of x32 syscalls. The syscalls common
  141. * with x86_64 obviously do not need such care.
  142. */
  143. #define __X32_COMPAT_SYS_STUB0(name) \
  144. __SYS_STUB0(x64, compat_sys_##name)
  145. #define __X32_COMPAT_SYS_STUBx(x, name, ...) \
  146. __SYS_STUBx(x64, compat_sys##name, \
  147. SC_X86_64_REGS_TO_ARGS(x, __VA_ARGS__))
  148. #define __X32_COMPAT_COND_SYSCALL(name) \
  149. __COND_SYSCALL(x64, compat_sys_##name)
  150. #define __X32_COMPAT_SYS_NI(name) \
  151. __SYS_NI(x64, compat_sys_##name)
  152. #else /* CONFIG_X86_X32_ABI */
  153. #define __X32_COMPAT_SYS_STUB0(name)
  154. #define __X32_COMPAT_SYS_STUBx(x, name, ...)
  155. #define __X32_COMPAT_COND_SYSCALL(name)
  156. #define __X32_COMPAT_SYS_NI(name)
  157. #endif /* CONFIG_X86_X32_ABI */
  158. #ifdef CONFIG_COMPAT
  159. /*
  160. * Compat means IA32_EMULATION and/or X86_X32. As they use a different
  161. * mapping of registers to parameters, we need to generate stubs for each
  162. * of them.
  163. */
  164. #define COMPAT_SYSCALL_DEFINE0(name) \
  165. static long \
  166. __do_compat_sys_##name(const struct pt_regs *__unused); \
  167. __IA32_COMPAT_SYS_STUB0(name) \
  168. __X32_COMPAT_SYS_STUB0(name) \
  169. static long \
  170. __do_compat_sys_##name(const struct pt_regs *__unused)
  171. #define COMPAT_SYSCALL_DEFINEx(x, name, ...) \
  172. static long __se_compat_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__)); \
  173. static inline long __do_compat_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__));\
  174. __IA32_COMPAT_SYS_STUBx(x, name, __VA_ARGS__) \
  175. __X32_COMPAT_SYS_STUBx(x, name, __VA_ARGS__) \
  176. static long __se_compat_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__)) \
  177. { \
  178. return __do_compat_sys##name(__MAP(x,__SC_DELOUSE,__VA_ARGS__));\
  179. } \
  180. static inline long __do_compat_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))
  181. /*
  182. * As some compat syscalls may not be implemented, we need to expand
  183. * COND_SYSCALL_COMPAT in kernel/sys_ni.c and COMPAT_SYS_NI in
  184. * kernel/time/posix-stubs.c to cover this case as well.
  185. */
  186. #define COND_SYSCALL_COMPAT(name) \
  187. __IA32_COMPAT_COND_SYSCALL(name) \
  188. __X32_COMPAT_COND_SYSCALL(name)
  189. #define COMPAT_SYS_NI(name) \
  190. __IA32_COMPAT_SYS_NI(name) \
  191. __X32_COMPAT_SYS_NI(name)
  192. #endif /* CONFIG_COMPAT */
  193. #define __SYSCALL_DEFINEx(x, name, ...) \
  194. static long __se_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__)); \
  195. static inline long __do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__));\
  196. __X64_SYS_STUBx(x, name, __VA_ARGS__) \
  197. __IA32_SYS_STUBx(x, name, __VA_ARGS__) \
  198. static long __se_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__)) \
  199. { \
  200. long ret = __do_sys##name(__MAP(x,__SC_CAST,__VA_ARGS__));\
  201. __MAP(x,__SC_TEST,__VA_ARGS__); \
  202. __PROTECT(x, ret,__MAP(x,__SC_ARGS,__VA_ARGS__)); \
  203. return ret; \
  204. } \
  205. static inline long __do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))
  206. /*
  207. * As the generic SYSCALL_DEFINE0() macro does not decode any parameters for
  208. * obvious reasons, and passing struct pt_regs *regs to it in %rdi does not
  209. * hurt, we only need to re-define it here to keep the naming congruent to
  210. * SYSCALL_DEFINEx() -- which is essential for the COND_SYSCALL() and SYS_NI()
  211. * macros to work correctly.
  212. */
  213. #define SYSCALL_DEFINE0(sname) \
  214. SYSCALL_METADATA(_##sname, 0); \
  215. static long __do_sys_##sname(const struct pt_regs *__unused); \
  216. __X64_SYS_STUB0(sname) \
  217. __IA32_SYS_STUB0(sname) \
  218. static long __do_sys_##sname(const struct pt_regs *__unused)
  219. #define COND_SYSCALL(name) \
  220. __X64_COND_SYSCALL(name) \
  221. __IA32_COND_SYSCALL(name)
  222. #define SYS_NI(name) \
  223. __X64_SYS_NI(name) \
  224. __IA32_SYS_NI(name)
  225. /*
  226. * For VSYSCALLS, we need to declare these three syscalls with the new
  227. * pt_regs-based calling convention for in-kernel use.
  228. */
  229. long __x64_sys_getcpu(const struct pt_regs *regs);
  230. long __x64_sys_gettimeofday(const struct pt_regs *regs);
  231. long __x64_sys_time(const struct pt_regs *regs);
  232. #endif /* _ASM_X86_SYSCALL_WRAPPER_H */