xref: /openbmc/linux/arch/mips/include/asm/syscall.h (revision 85cc91e2)
119e2e172SRalf Baechle /*
2c0ff3c53SRalf Baechle  * Access to user system call parameters and results
3c0ff3c53SRalf Baechle  *
419e2e172SRalf Baechle  * This file is subject to the terms and conditions of the GNU General Public
519e2e172SRalf Baechle  * License.  See the file "COPYING" in the main directory of this archive
619e2e172SRalf Baechle  * for more details.
719e2e172SRalf Baechle  *
8c0ff3c53SRalf Baechle  * See asm-generic/syscall.h for descriptions of what we must do here.
9c0ff3c53SRalf Baechle  *
1019e2e172SRalf Baechle  * Copyright (C) 2012 Ralf Baechle <ralf@linux-mips.org>
1119e2e172SRalf Baechle  */
1219e2e172SRalf Baechle 
1319e2e172SRalf Baechle #ifndef __ASM_MIPS_SYSCALL_H
1419e2e172SRalf Baechle #define __ASM_MIPS_SYSCALL_H
1519e2e172SRalf Baechle 
16f5179287SRalf Baechle #include <linux/compiler.h>
17579ec9e1SEric Paris #include <uapi/linux/audit.h>
18bec9b2b2SRalf Baechle #include <linux/elf-em.h>
19c0ff3c53SRalf Baechle #include <linux/kernel.h>
20c0ff3c53SRalf Baechle #include <linux/sched.h>
21c0ff3c53SRalf Baechle #include <linux/uaccess.h>
22c0ff3c53SRalf Baechle #include <asm/ptrace.h>
234c21b8fdSMarkos Chandras #include <asm/unistd.h>
244c21b8fdSMarkos Chandras 
254c21b8fdSMarkos Chandras #ifndef __NR_syscall /* Only defined if _MIPS_SIM == _MIPS_SIM_ABI32 */
264c21b8fdSMarkos Chandras #define __NR_syscall 4000
274c21b8fdSMarkos Chandras #endif
28c0ff3c53SRalf Baechle 
mips_syscall_is_indirect(struct task_struct * task,struct pt_regs * regs)29de8cd0dcSJames Hogan static inline bool mips_syscall_is_indirect(struct task_struct *task,
30de8cd0dcSJames Hogan 					    struct pt_regs *regs)
31de8cd0dcSJames Hogan {
32de8cd0dcSJames Hogan 	/* O32 ABI syscall() - Either 64-bit with O32 or 32-bit */
33de8cd0dcSJames Hogan 	return (IS_ENABLED(CONFIG_32BIT) ||
34de8cd0dcSJames Hogan 		test_tsk_thread_flag(task, TIF_32BIT_REGS)) &&
35de8cd0dcSJames Hogan 		(regs->regs[2] == __NR_syscall);
36de8cd0dcSJames Hogan }
37de8cd0dcSJames Hogan 
syscall_get_nr(struct task_struct * task,struct pt_regs * regs)38c0ff3c53SRalf Baechle static inline long syscall_get_nr(struct task_struct *task,
39c0ff3c53SRalf Baechle 				  struct pt_regs *regs)
40c0ff3c53SRalf Baechle {
41*85cc91e2SElvira Khabirova 	return task_thread_info(task)->syscall;
42c0ff3c53SRalf Baechle }
43c0ff3c53SRalf Baechle 
mips_syscall_update_nr(struct task_struct * task,struct pt_regs * regs)44de8cd0dcSJames Hogan static inline void mips_syscall_update_nr(struct task_struct *task,
45de8cd0dcSJames Hogan 					  struct pt_regs *regs)
46de8cd0dcSJames Hogan {
47de8cd0dcSJames Hogan 	/*
48de8cd0dcSJames Hogan 	 * v0 is the system call number, except for O32 ABI syscall(), where it
49de8cd0dcSJames Hogan 	 * ends up in a0.
50de8cd0dcSJames Hogan 	 */
51de8cd0dcSJames Hogan 	if (mips_syscall_is_indirect(task, regs))
52de8cd0dcSJames Hogan 		task_thread_info(task)->syscall = regs->regs[4];
53de8cd0dcSJames Hogan 	else
54de8cd0dcSJames Hogan 		task_thread_info(task)->syscall = regs->regs[2];
55de8cd0dcSJames Hogan }
56de8cd0dcSJames Hogan 
mips_get_syscall_arg(unsigned long * arg,struct task_struct * task,struct pt_regs * regs,unsigned int n)57077ff3beSNathan Chancellor static inline void mips_get_syscall_arg(unsigned long *arg,
58c0ff3c53SRalf Baechle 	struct task_struct *task, struct pt_regs *regs, unsigned int n)
59c0ff3c53SRalf Baechle {
6063238f2cSGuenter Roeck 	unsigned long usp __maybe_unused = regs->regs[29];
61c0ff3c53SRalf Baechle 
62c0ff3c53SRalf Baechle 	switch (n) {
63c0ff3c53SRalf Baechle 	case 0: case 1: case 2: case 3:
64c0ff3c53SRalf Baechle 		*arg = regs->regs[4 + n];
65c0ff3c53SRalf Baechle 
66077ff3beSNathan Chancellor 		return;
67c0ff3c53SRalf Baechle 
68c0ff3c53SRalf Baechle #ifdef CONFIG_32BIT
69c0ff3c53SRalf Baechle 	case 4: case 5: case 6: case 7:
70077ff3beSNathan Chancellor 		get_user(*arg, (int *)usp + n);
71077ff3beSNathan Chancellor 		return;
72c0ff3c53SRalf Baechle #endif
73c0ff3c53SRalf Baechle 
74c0ff3c53SRalf Baechle #ifdef CONFIG_64BIT
75c0ff3c53SRalf Baechle 	case 4: case 5: case 6: case 7:
76c0ff3c53SRalf Baechle #ifdef CONFIG_MIPS32_O32
77c50cbd85SDmitry V. Levin 		if (test_tsk_thread_flag(task, TIF_32BIT_REGS))
78077ff3beSNathan Chancellor 			get_user(*arg, (int *)usp + n);
79c0ff3c53SRalf Baechle 		else
80c0ff3c53SRalf Baechle #endif
81c0ff3c53SRalf Baechle 			*arg = regs->regs[4 + n];
82c0ff3c53SRalf Baechle 
83077ff3beSNathan Chancellor 		return;
84c0ff3c53SRalf Baechle #endif
85c0ff3c53SRalf Baechle 
86c0ff3c53SRalf Baechle 	default:
87c0ff3c53SRalf Baechle 		BUG();
88c0ff3c53SRalf Baechle 	}
89f5179287SRalf Baechle 
90f5179287SRalf Baechle 	unreachable();
91c0ff3c53SRalf Baechle }
92c0ff3c53SRalf Baechle 
syscall_get_error(struct task_struct * task,struct pt_regs * regs)93ba849160SDmitry V. Levin static inline long syscall_get_error(struct task_struct *task,
94ba849160SDmitry V. Levin 				     struct pt_regs *regs)
95ba849160SDmitry V. Levin {
96ba849160SDmitry V. Levin 	return regs->regs[7] ? -regs->regs[2] : 0;
97ba849160SDmitry V. Levin }
98ba849160SDmitry V. Levin 
syscall_get_return_value(struct task_struct * task,struct pt_regs * regs)991d7bf993SRalf Baechle static inline long syscall_get_return_value(struct task_struct *task,
1001d7bf993SRalf Baechle 					    struct pt_regs *regs)
1011d7bf993SRalf Baechle {
1021d7bf993SRalf Baechle 	return regs->regs[2];
1031d7bf993SRalf Baechle }
1041d7bf993SRalf Baechle 
syscall_rollback(struct task_struct * task,struct pt_regs * regs)10522feadbeSMarkos Chandras static inline void syscall_rollback(struct task_struct *task,
10622feadbeSMarkos Chandras 				    struct pt_regs *regs)
10722feadbeSMarkos Chandras {
10822feadbeSMarkos Chandras 	/* Do nothing */
10922feadbeSMarkos Chandras }
11022feadbeSMarkos Chandras 
syscall_set_return_value(struct task_struct * task,struct pt_regs * regs,int error,long val)1111d7bf993SRalf Baechle static inline void syscall_set_return_value(struct task_struct *task,
1121d7bf993SRalf Baechle 					    struct pt_regs *regs,
1131d7bf993SRalf Baechle 					    int error, long val)
1141d7bf993SRalf Baechle {
1151d7bf993SRalf Baechle 	if (error) {
1161d7bf993SRalf Baechle 		regs->regs[2] = -error;
117becddba9SJames Hogan 		regs->regs[7] = 1;
1181d7bf993SRalf Baechle 	} else {
1191d7bf993SRalf Baechle 		regs->regs[2] = val;
1201d7bf993SRalf Baechle 		regs->regs[7] = 0;
1211d7bf993SRalf Baechle 	}
1221d7bf993SRalf Baechle }
1231d7bf993SRalf Baechle 
syscall_get_arguments(struct task_struct * task,struct pt_regs * regs,unsigned long * args)124c0ff3c53SRalf Baechle static inline void syscall_get_arguments(struct task_struct *task,
125c0ff3c53SRalf Baechle 					 struct pt_regs *regs,
126c0ff3c53SRalf Baechle 					 unsigned long *args)
127c0ff3c53SRalf Baechle {
128b35f549dSSteven Rostedt (Red Hat) 	unsigned int i = 0;
129b35f549dSSteven Rostedt (Red Hat) 	unsigned int n = 6;
130de8cd0dcSJames Hogan 
131de8cd0dcSJames Hogan 	/* O32 ABI syscall() */
132de8cd0dcSJames Hogan 	if (mips_syscall_is_indirect(task, regs))
1334c21b8fdSMarkos Chandras 		i++;
134c0ff3c53SRalf Baechle 
135c0ff3c53SRalf Baechle 	while (n--)
136077ff3beSNathan Chancellor 		mips_get_syscall_arg(args++, task, regs, i++);
137c0ff3c53SRalf Baechle }
138c0ff3c53SRalf Baechle 
13919e2e172SRalf Baechle extern const unsigned long sys_call_table[];
14019e2e172SRalf Baechle extern const unsigned long sys32_call_table[];
14119e2e172SRalf Baechle extern const unsigned long sysn32_call_table[];
14219e2e172SRalf Baechle 
syscall_get_arch(struct task_struct * task)14316add411SDmitry V. Levin static inline int syscall_get_arch(struct task_struct *task)
144bec9b2b2SRalf Baechle {
145ce5d1128SEric Paris 	int arch = AUDIT_ARCH_MIPS;
146bec9b2b2SRalf Baechle #ifdef CONFIG_64BIT
14716add411SDmitry V. Levin 	if (!test_tsk_thread_flag(task, TIF_32BIT_REGS)) {
148bec9b2b2SRalf Baechle 		arch |= __AUDIT_ARCH_64BIT;
14940381529SMarkos Chandras 		/* N32 sets only TIF_32BIT_ADDR */
15016add411SDmitry V. Levin 		if (test_tsk_thread_flag(task, TIF_32BIT_ADDR))
151f2d0801fSMarkos Chandras 			arch |= __AUDIT_ARCH_CONVENTION_MIPS64_N32;
15240381529SMarkos Chandras 	}
153bec9b2b2SRalf Baechle #endif
154bec9b2b2SRalf Baechle #if defined(__LITTLE_ENDIAN)
155bec9b2b2SRalf Baechle 	arch |=  __AUDIT_ARCH_LE;
156bec9b2b2SRalf Baechle #endif
157bec9b2b2SRalf Baechle 	return arch;
158bec9b2b2SRalf Baechle }
159bec9b2b2SRalf Baechle 
16019e2e172SRalf Baechle #endif	/* __ASM_MIPS_SYSCALL_H */
161