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 <[email protected]>
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 
mips_syscall_update_nr(struct task_struct * task,struct pt_regs * regs)44 static inline void mips_syscall_update_nr(struct task_struct *task,
45 					  struct pt_regs *regs)
46 {
47 	/*
48 	 * v0 is the system call number, except for O32 ABI syscall(), where it
49 	 * ends up in a0.
50 	 */
51 	if (mips_syscall_is_indirect(task, regs))
52 		task_thread_info(task)->syscall = regs->regs[4];
53 	else
54 		task_thread_info(task)->syscall = regs->regs[2];
55 }
56 
mips_get_syscall_arg(unsigned long * arg,struct task_struct * task,struct pt_regs * regs,unsigned int n)57 static inline void mips_get_syscall_arg(unsigned long *arg,
58 	struct task_struct *task, struct pt_regs *regs, unsigned int n)
59 {
60 #ifdef CONFIG_32BIT
61 	switch (n) {
62 	case 0: case 1: case 2: case 3:
63 		*arg = regs->regs[4 + n];
64 		return;
65 	case 4: case 5: case 6: case 7:
66 		*arg = regs->args[n];
67 		return;
68 	}
69 #else
70 	*arg = regs->regs[4 + n];
71 	if ((IS_ENABLED(CONFIG_MIPS32_O32) &&
72 	     test_tsk_thread_flag(task, TIF_32BIT_REGS)))
73 		*arg = (unsigned int)*arg;
74 #endif
75 }
76 
syscall_get_error(struct task_struct * task,struct pt_regs * regs)77 static inline long syscall_get_error(struct task_struct *task,
78 				     struct pt_regs *regs)
79 {
80 	return regs->regs[7] ? -regs->regs[2] : 0;
81 }
82 
syscall_get_return_value(struct task_struct * task,struct pt_regs * regs)83 static inline long syscall_get_return_value(struct task_struct *task,
84 					    struct pt_regs *regs)
85 {
86 	return regs->regs[2];
87 }
88 
syscall_rollback(struct task_struct * task,struct pt_regs * regs)89 static inline void syscall_rollback(struct task_struct *task,
90 				    struct pt_regs *regs)
91 {
92 	/* Do nothing */
93 }
94 
syscall_set_return_value(struct task_struct * task,struct pt_regs * regs,int error,long val)95 static inline void syscall_set_return_value(struct task_struct *task,
96 					    struct pt_regs *regs,
97 					    int error, long val)
98 {
99 	if (error) {
100 		regs->regs[2] = -error;
101 		regs->regs[7] = 1;
102 	} else {
103 		regs->regs[2] = val;
104 		regs->regs[7] = 0;
105 	}
106 }
107 
syscall_get_arguments(struct task_struct * task,struct pt_regs * regs,unsigned long * args)108 static inline void syscall_get_arguments(struct task_struct *task,
109 					 struct pt_regs *regs,
110 					 unsigned long *args)
111 {
112 	unsigned int i = 0;
113 	unsigned int n = 6;
114 
115 	/* O32 ABI syscall() */
116 	if (mips_syscall_is_indirect(task, regs))
117 		i++;
118 
119 	while (n--)
120 		mips_get_syscall_arg(args++, task, regs, i++);
121 }
122 
123 extern const unsigned long sys_call_table[];
124 extern const unsigned long sys32_call_table[];
125 extern const unsigned long sysn32_call_table[];
126 
syscall_get_arch(struct task_struct * task)127 static inline int syscall_get_arch(struct task_struct *task)
128 {
129 	int arch = AUDIT_ARCH_MIPS;
130 #ifdef CONFIG_64BIT
131 	if (!test_tsk_thread_flag(task, TIF_32BIT_REGS)) {
132 		arch |= __AUDIT_ARCH_64BIT;
133 		/* N32 sets only TIF_32BIT_ADDR */
134 		if (test_tsk_thread_flag(task, TIF_32BIT_ADDR))
135 			arch |= __AUDIT_ARCH_CONVENTION_MIPS64_N32;
136 	}
137 #endif
138 #if defined(__LITTLE_ENDIAN)
139 	arch |=  __AUDIT_ARCH_LE;
140 #endif
141 	return arch;
142 }
143 
144 #endif	/* __ASM_MIPS_SYSCALL_H */
145