1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * syscall_wrapper.h - riscv specific wrappers to syscall definitions
4  *
5  * Based on arch/arm64/include/syscall_wrapper.h
6  */
7 
8 #ifndef __ASM_SYSCALL_WRAPPER_H
9 #define __ASM_SYSCALL_WRAPPER_H
10 
11 #include <asm/ptrace.h>
12 
13 asmlinkage long __riscv_sys_ni_syscall(const struct pt_regs *);
14 
15 #ifdef CONFIG_64BIT
16 
17 #define __SYSCALL_SE_DEFINEx(x, prefix, name, ...)					\
18 	static long __se_##prefix##name(__MAP(x,__SC_LONG,__VA_ARGS__));		\
19 	static long __se_##prefix##name(__MAP(x,__SC_LONG,__VA_ARGS__))
20 
21 #define SC_RISCV_REGS_TO_ARGS(x, ...)							\
22 	__MAP(x,__SC_ARGS								\
23 	      ,,regs->orig_a0,,regs->a1,,regs->a2					\
24 	      ,,regs->a3,,regs->a4,,regs->a5,,regs->a6)
25 
26 #else
27 /*
28  * Use type aliasing to ensure registers a0-a6 are correctly passed to the syscall
29  * implementation when >word-size arguments are used.
30  */
31 #define __SYSCALL_SE_DEFINEx(x, prefix, name, ...)					\
32 	__diag_push();									\
33 	__diag_ignore(GCC, 8, "-Wattribute-alias",					\
34 			"Type aliasing is used to sanitize syscall arguments");		\
35 	static long __se_##prefix##name(ulong, ulong, ulong, ulong, ulong, ulong, 	\
36 					ulong)						\
37 			__attribute__((alias(__stringify(___se_##prefix##name))));	\
38 	__diag_pop();									\
39 	static long noinline ___se_##prefix##name(__MAP(x,__SC_LONG,__VA_ARGS__))	\
40 			__used;								\
41 	static long ___se_##prefix##name(__MAP(x,__SC_LONG,__VA_ARGS__))
42 
43 #define SC_RISCV_REGS_TO_ARGS(x, ...) \
44 	regs->orig_a0,regs->a1,regs->a2,regs->a3,regs->a4,regs->a5,regs->a6
45 
46 #endif /* CONFIG_64BIT */
47 
48 #ifdef CONFIG_COMPAT
49 
50 #define COMPAT_SYSCALL_DEFINEx(x, name, ...)						\
51 	asmlinkage long __riscv_compat_sys##name(const struct pt_regs *regs);		\
52 	ALLOW_ERROR_INJECTION(__riscv_compat_sys##name, ERRNO);				\
53 	static inline long __do_compat_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__));	\
54 	__SYSCALL_SE_DEFINEx(x, compat_sys, name, __VA_ARGS__)				\
55 	{										\
56 		return __do_compat_sys##name(__MAP(x,__SC_DELOUSE,__VA_ARGS__));	\
57 	}										\
58 	asmlinkage long __riscv_compat_sys##name(const struct pt_regs *regs)		\
59 	{										\
60 		return __se_compat_sys##name(SC_RISCV_REGS_TO_ARGS(x,__VA_ARGS__));	\
61 	}										\
62 	static inline long __do_compat_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))
63 
64 #define COMPAT_SYSCALL_DEFINE0(sname)							\
65 	asmlinkage long __riscv_compat_sys_##sname(const struct pt_regs *__unused);	\
66 	ALLOW_ERROR_INJECTION(__riscv_compat_sys_##sname, ERRNO);			\
67 	asmlinkage long __riscv_compat_sys_##sname(const struct pt_regs *__unused)
68 
69 #define COND_SYSCALL_COMPAT(name) 							\
70 	asmlinkage long __weak __riscv_compat_sys_##name(const struct pt_regs *regs);	\
71 	asmlinkage long __weak __riscv_compat_sys_##name(const struct pt_regs *regs)	\
72 	{										\
73 		return sys_ni_syscall();						\
74 	}
75 
76 #endif /* CONFIG_COMPAT */
77 
78 #define __SYSCALL_DEFINEx(x, name, ...)						\
79 	asmlinkage long __riscv_sys##name(const struct pt_regs *regs);		\
80 	ALLOW_ERROR_INJECTION(__riscv_sys##name, ERRNO);			\
81 	static inline long __do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__));	\
82 	__SYSCALL_SE_DEFINEx(x, sys, name, __VA_ARGS__)				\
83 	{									\
84 		long ret = __do_sys##name(__MAP(x,__SC_CAST,__VA_ARGS__));	\
85 		__MAP(x,__SC_TEST,__VA_ARGS__);					\
86 		__PROTECT(x, ret,__MAP(x,__SC_ARGS,__VA_ARGS__));		\
87 		return ret;							\
88 	}									\
89 	asmlinkage long __riscv_sys##name(const struct pt_regs *regs)		\
90 	{									\
91 		return __se_sys##name(SC_RISCV_REGS_TO_ARGS(x,__VA_ARGS__));	\
92 	}									\
93 	static inline long __do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))
94 
95 #define SYSCALL_DEFINE0(sname)							\
96 	SYSCALL_METADATA(_##sname, 0);						\
97 	asmlinkage long __riscv_sys_##sname(const struct pt_regs *__unused);	\
98 	ALLOW_ERROR_INJECTION(__riscv_sys_##sname, ERRNO);			\
99 	asmlinkage long __riscv_sys_##sname(const struct pt_regs *__unused)
100 
101 #define COND_SYSCALL(name)							\
102 	asmlinkage long __weak __riscv_sys_##name(const struct pt_regs *regs);	\
103 	asmlinkage long __weak __riscv_sys_##name(const struct pt_regs *regs)	\
104 	{									\
105 		return sys_ni_syscall();					\
106 	}
107 
108 #endif /* __ASM_SYSCALL_WRAPPER_H */
109