xref: /linux/arch/mips/include/asm/syscall.h (revision 00c010e130e58301db2ea0cec1eadc931e1cb8cf)
1 /*
2  * Access to user system call parameters and results
3  *
4  * This file is subject to the terms and conditions of the GNU General Public
5  * License.  See the file "COPYING" in the main directory of this archive
6  * for more details.
7  *
8  * See asm-generic/syscall.h for descriptions of what we must do here.
9  *
10  * Copyright (C) 2012 Ralf Baechle <ralf@linux-mips.org>
11  */
12 
13 #ifndef __ASM_MIPS_SYSCALL_H
14 #define __ASM_MIPS_SYSCALL_H
15 
16 #include <linux/compiler.h>
17 #include <uapi/linux/audit.h>
18 #include <linux/elf-em.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/uaccess.h>
22 #include <asm/ptrace.h>
23 #include <asm/unistd.h>
24 
25 #ifndef __NR_syscall /* Only defined if _MIPS_SIM == _MIPS_SIM_ABI32 */
26 #define __NR_syscall 4000
27 #endif
28 
mips_syscall_is_indirect(struct task_struct * task,struct pt_regs * regs)29 static inline bool mips_syscall_is_indirect(struct task_struct *task,
30 					    struct pt_regs *regs)
31 {
32 	/* O32 ABI syscall() - Either 64-bit with O32 or 32-bit */
33 	return (IS_ENABLED(CONFIG_32BIT) ||
34 		test_tsk_thread_flag(task, TIF_32BIT_REGS)) &&
35 		(regs->regs[2] == __NR_syscall);
36 }
37 
syscall_get_nr(struct task_struct * task,struct pt_regs * regs)38 static inline long syscall_get_nr(struct task_struct *task,
39 				  struct pt_regs *regs)
40 {
41 	return task_thread_info(task)->syscall;
42 }
43 
syscall_set_nr(struct task_struct * task,struct pt_regs * regs,int nr)44 static inline void syscall_set_nr(struct task_struct *task,
45 				  struct pt_regs *regs,
46 				  int nr)
47 {
48 	/*
49 	 * New syscall number has to be assigned to regs[2] because
50 	 * it is loaded from there unconditionally after return from
51 	 * syscall_trace_enter() invocation.
52 	 *
53 	 * Consequently, if the syscall was indirect and nr != __NR_syscall,
54 	 * then after this assignment the syscall will cease to be indirect.
55 	 */
56 	task_thread_info(task)->syscall = regs->regs[2] = nr;
57 }
58 
mips_syscall_update_nr(struct task_struct * task,struct pt_regs * regs)59 static inline void mips_syscall_update_nr(struct task_struct *task,
60 					  struct pt_regs *regs)
61 {
62 	/*
63 	 * v0 is the system call number, except for O32 ABI syscall(), where it
64 	 * ends up in a0.
65 	 */
66 	if (mips_syscall_is_indirect(task, regs))
67 		task_thread_info(task)->syscall = regs->regs[4];
68 	else
69 		task_thread_info(task)->syscall = regs->regs[2];
70 }
71 
mips_get_syscall_arg(unsigned long * arg,struct task_struct * task,struct pt_regs * regs,unsigned int n)72 static inline void mips_get_syscall_arg(unsigned long *arg,
73 	struct task_struct *task, struct pt_regs *regs, unsigned int n)
74 {
75 #ifdef CONFIG_32BIT
76 	switch (n) {
77 	case 0: case 1: case 2: case 3:
78 		*arg = regs->regs[4 + n];
79 		return;
80 	case 4: case 5: case 6: case 7:
81 		*arg = regs->args[n];
82 		return;
83 	}
84 #else
85 	*arg = regs->regs[4 + n];
86 	if ((IS_ENABLED(CONFIG_MIPS32_O32) &&
87 	     test_tsk_thread_flag(task, TIF_32BIT_REGS)))
88 		*arg = (unsigned int)*arg;
89 #endif
90 }
91 
mips_set_syscall_arg(unsigned long * arg,struct task_struct * task,struct pt_regs * regs,unsigned int n)92 static inline void mips_set_syscall_arg(unsigned long *arg,
93 	struct task_struct *task, struct pt_regs *regs, unsigned int n)
94 {
95 #ifdef CONFIG_32BIT
96 	switch (n) {
97 	case 0: case 1: case 2: case 3:
98 		regs->regs[4 + n] = *arg;
99 		return;
100 	case 4: case 5: case 6: case 7:
101 		*arg = regs->args[n] = *arg;
102 		return;
103 	}
104 #else
105 	regs->regs[4 + n] = *arg;
106 #endif
107 }
108 
syscall_get_error(struct task_struct * task,struct pt_regs * regs)109 static inline long syscall_get_error(struct task_struct *task,
110 				     struct pt_regs *regs)
111 {
112 	return regs->regs[7] ? -regs->regs[2] : 0;
113 }
114 
syscall_get_return_value(struct task_struct * task,struct pt_regs * regs)115 static inline long syscall_get_return_value(struct task_struct *task,
116 					    struct pt_regs *regs)
117 {
118 	return regs->regs[2];
119 }
120 
syscall_rollback(struct task_struct * task,struct pt_regs * regs)121 static inline void syscall_rollback(struct task_struct *task,
122 				    struct pt_regs *regs)
123 {
124 	/* Do nothing */
125 }
126 
syscall_set_return_value(struct task_struct * task,struct pt_regs * regs,int error,long val)127 static inline void syscall_set_return_value(struct task_struct *task,
128 					    struct pt_regs *regs,
129 					    int error, long val)
130 {
131 	if (error) {
132 		regs->regs[2] = -error;
133 		regs->regs[7] = 1;
134 	} else {
135 		regs->regs[2] = val;
136 		regs->regs[7] = 0;
137 	}
138 }
139 
syscall_get_arguments(struct task_struct * task,struct pt_regs * regs,unsigned long * args)140 static inline void syscall_get_arguments(struct task_struct *task,
141 					 struct pt_regs *regs,
142 					 unsigned long *args)
143 {
144 	unsigned int i = 0;
145 	unsigned int n = 6;
146 
147 	/* O32 ABI syscall() */
148 	if (mips_syscall_is_indirect(task, regs))
149 		i++;
150 
151 	while (n--)
152 		mips_get_syscall_arg(args++, task, regs, i++);
153 }
154 
syscall_set_arguments(struct task_struct * task,struct pt_regs * regs,unsigned long * args)155 static inline void syscall_set_arguments(struct task_struct *task,
156 					 struct pt_regs *regs,
157 					 unsigned long *args)
158 {
159 	unsigned int i = 0;
160 	unsigned int n = 6;
161 
162 	while (n--)
163 		mips_set_syscall_arg(args++, task, regs, i++);
164 }
165 
166 extern const unsigned long sys_call_table[];
167 extern const unsigned long sys32_call_table[];
168 extern const unsigned long sysn32_call_table[];
169 
syscall_get_arch(struct task_struct * task)170 static inline int syscall_get_arch(struct task_struct *task)
171 {
172 	int arch = AUDIT_ARCH_MIPS;
173 #ifdef CONFIG_64BIT
174 	if (!test_tsk_thread_flag(task, TIF_32BIT_REGS)) {
175 		arch |= __AUDIT_ARCH_64BIT;
176 		/* N32 sets only TIF_32BIT_ADDR */
177 		if (test_tsk_thread_flag(task, TIF_32BIT_ADDR))
178 			arch |= __AUDIT_ARCH_CONVENTION_MIPS64_N32;
179 	}
180 #endif
181 #if defined(__LITTLE_ENDIAN)
182 	arch |=  __AUDIT_ARCH_LE;
183 #endif
184 	return arch;
185 }
186 
187 #endif	/* __ASM_MIPS_SYSCALL_H */
188