xref: /openbmc/linux/arch/s390/include/asm/syscall.h (revision 2bdd5238)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Access to user system call parameters and results
4  *
5  *  Copyright IBM Corp. 2008
6  *  Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
7  */
8 
9 #ifndef _ASM_SYSCALL_H
10 #define _ASM_SYSCALL_H	1
11 
12 #include <uapi/linux/audit.h>
13 #include <linux/sched.h>
14 #include <linux/err.h>
15 #include <asm/ptrace.h>
16 
17 extern const sys_call_ptr_t sys_call_table[];
18 extern const sys_call_ptr_t sys_call_table_emu[];
19 
20 static inline long syscall_get_nr(struct task_struct *task,
21 				  struct pt_regs *regs)
22 {
23 	return test_pt_regs_flag(regs, PIF_SYSCALL) ?
24 		(regs->int_code & 0xffff) : -1;
25 }
26 
27 static inline void syscall_rollback(struct task_struct *task,
28 				    struct pt_regs *regs)
29 {
30 	regs->gprs[2] = regs->orig_gpr2;
31 }
32 
33 static inline long syscall_get_error(struct task_struct *task,
34 				     struct pt_regs *regs)
35 {
36 	unsigned long error = regs->gprs[2];
37 #ifdef CONFIG_COMPAT
38 	if (test_tsk_thread_flag(task, TIF_31BIT)) {
39 		/*
40 		 * Sign-extend the value so (int)-EFOO becomes (long)-EFOO
41 		 * and will match correctly in comparisons.
42 		 */
43 		error = (long)(int)error;
44 	}
45 #endif
46 	return IS_ERR_VALUE(error) ? error : 0;
47 }
48 
49 static inline long syscall_get_return_value(struct task_struct *task,
50 					    struct pt_regs *regs)
51 {
52 	return regs->gprs[2];
53 }
54 
55 static inline void syscall_set_return_value(struct task_struct *task,
56 					    struct pt_regs *regs,
57 					    int error, long val)
58 {
59 	set_pt_regs_flag(regs, PIF_SYSCALL_RET_SET);
60 	regs->gprs[2] = error ? error : val;
61 }
62 
63 static inline void syscall_get_arguments(struct task_struct *task,
64 					 struct pt_regs *regs,
65 					 unsigned long *args)
66 {
67 	unsigned long mask = -1UL;
68 	unsigned int n = 6;
69 
70 #ifdef CONFIG_COMPAT
71 	if (test_tsk_thread_flag(task, TIF_31BIT))
72 		mask = 0xffffffff;
73 #endif
74 	while (n-- > 0)
75 		if (n > 0)
76 			args[n] = regs->gprs[2 + n] & mask;
77 
78 	args[0] = regs->orig_gpr2 & mask;
79 }
80 
81 static inline void syscall_set_arguments(struct task_struct *task,
82 					 struct pt_regs *regs,
83 					 const unsigned long *args)
84 {
85 	unsigned int n = 6;
86 
87 	while (n-- > 0)
88 		if (n > 0)
89 			regs->gprs[2 + n] = args[n];
90 	regs->orig_gpr2 = args[0];
91 }
92 
93 static inline int syscall_get_arch(struct task_struct *task)
94 {
95 #ifdef CONFIG_COMPAT
96 	if (test_tsk_thread_flag(task, TIF_31BIT))
97 		return AUDIT_ARCH_S390;
98 #endif
99 	return AUDIT_ARCH_S390X;
100 }
101 
102 static inline bool arch_syscall_is_vdso_sigreturn(struct pt_regs *regs)
103 {
104 	return false;
105 }
106 
107 #define SYSCALL_FMT_0
108 #define SYSCALL_FMT_1 , "0" (r2)
109 #define SYSCALL_FMT_2 , "d" (r3) SYSCALL_FMT_1
110 #define SYSCALL_FMT_3 , "d" (r4) SYSCALL_FMT_2
111 #define SYSCALL_FMT_4 , "d" (r5) SYSCALL_FMT_3
112 #define SYSCALL_FMT_5 , "d" (r6) SYSCALL_FMT_4
113 #define SYSCALL_FMT_6 , "d" (r7) SYSCALL_FMT_5
114 
115 #define SYSCALL_PARM_0
116 #define SYSCALL_PARM_1 , long arg1
117 #define SYSCALL_PARM_2 SYSCALL_PARM_1, long arg2
118 #define SYSCALL_PARM_3 SYSCALL_PARM_2, long arg3
119 #define SYSCALL_PARM_4 SYSCALL_PARM_3, long arg4
120 #define SYSCALL_PARM_5 SYSCALL_PARM_4, long arg5
121 #define SYSCALL_PARM_6 SYSCALL_PARM_5, long arg6
122 
123 #define SYSCALL_REGS_0
124 #define SYSCALL_REGS_1							\
125 	register long r2 asm("2") = arg1
126 #define SYSCALL_REGS_2							\
127 	SYSCALL_REGS_1;							\
128 	register long r3 asm("3") = arg2
129 #define SYSCALL_REGS_3							\
130 	SYSCALL_REGS_2;							\
131 	register long r4 asm("4") = arg3
132 #define SYSCALL_REGS_4							\
133 	SYSCALL_REGS_3;							\
134 	register long r5 asm("5") = arg4
135 #define SYSCALL_REGS_5							\
136 	SYSCALL_REGS_4;							\
137 	register long r6 asm("6") = arg5
138 #define SYSCALL_REGS_6							\
139 	SYSCALL_REGS_5;							\
140 	register long r7 asm("7") = arg6
141 
142 #define GENERATE_SYSCALL_FUNC(nr)					\
143 static __always_inline							\
144 long syscall##nr(unsigned long syscall SYSCALL_PARM_##nr)		\
145 {									\
146 	register unsigned long r1 asm ("1") = syscall;			\
147 	register long rc asm ("2");					\
148 	SYSCALL_REGS_##nr;						\
149 									\
150 	asm volatile (							\
151 		"	svc	0\n"					\
152 		: "=d" (rc)						\
153 		: "d" (r1) SYSCALL_FMT_##nr				\
154 		: "memory");						\
155 	return rc;							\
156 }
157 
158 GENERATE_SYSCALL_FUNC(0)
159 GENERATE_SYSCALL_FUNC(1)
160 GENERATE_SYSCALL_FUNC(2)
161 GENERATE_SYSCALL_FUNC(3)
162 GENERATE_SYSCALL_FUNC(4)
163 GENERATE_SYSCALL_FUNC(5)
164 GENERATE_SYSCALL_FUNC(6)
165 
166 #endif	/* _ASM_SYSCALL_H */
167