xref: /openbmc/linux/arch/mips/kernel/syscall.c (revision 49955d84cd9ccdca5a16a495e448e1a06fad9e49)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * This file is subject to the terms and conditions of the GNU General Public
31da177e4SLinus Torvalds  * License.  See the file "COPYING" in the main directory of this archive
41da177e4SLinus Torvalds  * for more details.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (C) 1995, 1996, 1997, 2000, 2001, 05 by Ralf Baechle
71da177e4SLinus Torvalds  * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
81da177e4SLinus Torvalds  * Copyright (C) 2001 MIPS Technologies, Inc.
91da177e4SLinus Torvalds  */
10a9415644SRandy Dunlap #include <linux/capability.h>
111da177e4SLinus Torvalds #include <linux/errno.h>
121da177e4SLinus Torvalds #include <linux/linkage.h>
134e950f6fSAlexey Dobriyan #include <linux/fs.h>
141da177e4SLinus Torvalds #include <linux/smp.h>
151da177e4SLinus Torvalds #include <linux/ptrace.h>
161da177e4SLinus Torvalds #include <linux/string.h>
171da177e4SLinus Torvalds #include <linux/syscalls.h>
181da177e4SLinus Torvalds #include <linux/file.h>
191da177e4SLinus Torvalds #include <linux/utsname.h>
201da177e4SLinus Torvalds #include <linux/unistd.h>
211da177e4SLinus Torvalds #include <linux/sem.h>
221da177e4SLinus Torvalds #include <linux/msg.h>
231da177e4SLinus Torvalds #include <linux/shm.h>
241da177e4SLinus Torvalds #include <linux/compiler.h>
25cba4fbbfSAdrian Bunk #include <linux/ipc.h>
26f1e39a4aSRalf Baechle #include <linux/uaccess.h>
275a0e3ad6STejun Heo #include <linux/slab.h>
28652b14aaSDavid Daney #include <linux/elf.h>
2968db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
301da177e4SLinus Torvalds 
31f1e39a4aSRalf Baechle #include <asm/asm.h>
321da177e4SLinus Torvalds #include <asm/branch.h>
331da177e4SLinus Torvalds #include <asm/cachectl.h>
341da177e4SLinus Torvalds #include <asm/cacheflush.h>
35048eb582SSam Ravnborg #include <asm/asm-offsets.h>
361da177e4SLinus Torvalds #include <asm/signal.h>
371da177e4SLinus Torvalds #include <asm/sim.h>
381da177e4SLinus Torvalds #include <asm/shmparam.h>
391da177e4SLinus Torvalds #include <asm/sysmips.h>
40b81947c6SDavid Howells #include <asm/switch_to.h>
411da177e4SLinus Torvalds 
428213bbf9SRalf Baechle /*
438213bbf9SRalf Baechle  * For historic reasons the pipe(2) syscall on MIPS has an unusual calling
448213bbf9SRalf Baechle  * convention.	It returns results in registers $v0 / $v1 which means there
458213bbf9SRalf Baechle  * is no need for it to do verify the validity of a userspace pointer
468213bbf9SRalf Baechle  * argument.  Historically that used to be expensive in Linux.	These days
478213bbf9SRalf Baechle  * the performance advantage is negligible.
488213bbf9SRalf Baechle  */
49974fdb3cSAl Viro asmlinkage int sysm_pipe(void)
501da177e4SLinus Torvalds {
511da177e4SLinus Torvalds 	int fd[2];
52974fdb3cSAl Viro 	int error = do_pipe_flags(fd, 0);
53974fdb3cSAl Viro 	if (error)
54974fdb3cSAl Viro 		return error;
55974fdb3cSAl Viro 	current_pt_regs()->regs[3] = fd[1];
56974fdb3cSAl Viro 	return fd[0];
571da177e4SLinus Torvalds }
581da177e4SLinus Torvalds 
59dbda6ac0SRalf Baechle SYSCALL_DEFINE6(mips_mmap, unsigned long, addr, unsigned long, len,
60dbda6ac0SRalf Baechle 	unsigned long, prot, unsigned long, flags, unsigned long,
61dbda6ac0SRalf Baechle 	fd, off_t, offset)
621da177e4SLinus Torvalds {
631da177e4SLinus Torvalds 	if (offset & ~PAGE_MASK)
64c9d3fdf3SMarkus Elfring 		return -EINVAL;
65c9d3fdf3SMarkus Elfring 	return sys_mmap_pgoff(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
661da177e4SLinus Torvalds }
671da177e4SLinus Torvalds 
68dbda6ac0SRalf Baechle SYSCALL_DEFINE6(mips_mmap2, unsigned long, addr, unsigned long, len,
69dbda6ac0SRalf Baechle 	unsigned long, prot, unsigned long, flags, unsigned long, fd,
70dbda6ac0SRalf Baechle 	unsigned long, pgoff)
711da177e4SLinus Torvalds {
72947df17cSH. Peter Anvin 	if (pgoff & (~PAGE_MASK >> 12))
73947df17cSH. Peter Anvin 		return -EINVAL;
74947df17cSH. Peter Anvin 
75f8b72560SAl Viro 	return sys_mmap_pgoff(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT-12));
761da177e4SLinus Torvalds }
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds save_static_function(sys_fork);
791da177e4SLinus Torvalds save_static_function(sys_clone);
801da177e4SLinus Torvalds 
81dbda6ac0SRalf Baechle SYSCALL_DEFINE1(set_thread_area, unsigned long, addr)
823c37026dSRalf Baechle {
83dc8f6029SAl Viro 	struct thread_info *ti = task_thread_info(current);
843c37026dSRalf Baechle 
853c37026dSRalf Baechle 	ti->tp_value = addr;
86a3692020SRalf Baechle 	if (cpu_has_userlocal)
87a3692020SRalf Baechle 		write_c0_userlocal(addr);
8806be375bSRalf Baechle 
8906be375bSRalf Baechle 	return 0;
903c37026dSRalf Baechle }
913c37026dSRalf Baechle 
9212890d0fSRalf Baechle static inline int mips_atomic_set(unsigned long addr, unsigned long new)
931da177e4SLinus Torvalds {
94f1e39a4aSRalf Baechle 	unsigned long old, tmp;
9512890d0fSRalf Baechle 	struct pt_regs *regs;
96f1e39a4aSRalf Baechle 	unsigned int err;
97f1e39a4aSRalf Baechle 
98f1e39a4aSRalf Baechle 	if (unlikely(addr & 3))
99f1e39a4aSRalf Baechle 		return -EINVAL;
100f1e39a4aSRalf Baechle 
101f0a955f4SAl Viro 	if (unlikely(!access_ok(VERIFY_WRITE, (const void __user *)addr, 4)))
102f1e39a4aSRalf Baechle 		return -EINVAL;
103f1e39a4aSRalf Baechle 
104f1e39a4aSRalf Baechle 	if (cpu_has_llsc && R10000_LLSC_WAR) {
105f1e39a4aSRalf Baechle 		__asm__ __volatile__ (
106a809d460SRalf Baechle 		"	.set	arch=r4000				\n"
107f1e39a4aSRalf Baechle 		"	li	%[err], 0				\n"
108f1e39a4aSRalf Baechle 		"1:	ll	%[old], (%[addr])			\n"
109f1e39a4aSRalf Baechle 		"	move	%[tmp], %[new]				\n"
110f1e39a4aSRalf Baechle 		"2:	sc	%[tmp], (%[addr])			\n"
111f1e39a4aSRalf Baechle 		"	beqzl	%[tmp], 1b				\n"
112f1e39a4aSRalf Baechle 		"3:							\n"
1130e525e48SMaciej W. Rozycki 		"	.insn						\n"
114f1e39a4aSRalf Baechle 		"	.section .fixup,\"ax\"				\n"
115f1e39a4aSRalf Baechle 		"4:	li	%[err], %[efault]			\n"
116f1e39a4aSRalf Baechle 		"	j	3b					\n"
117f1e39a4aSRalf Baechle 		"	.previous					\n"
118f1e39a4aSRalf Baechle 		"	.section __ex_table,\"a\"			\n"
119f1e39a4aSRalf Baechle 		"	"STR(PTR)"	1b, 4b				\n"
120f1e39a4aSRalf Baechle 		"	"STR(PTR)"	2b, 4b				\n"
121f1e39a4aSRalf Baechle 		"	.previous					\n"
122a91be9eeSRalf Baechle 		"	.set	mips0					\n"
123f1e39a4aSRalf Baechle 		: [old] "=&r" (old),
124f1e39a4aSRalf Baechle 		  [err] "=&r" (err),
125f1e39a4aSRalf Baechle 		  [tmp] "=&r" (tmp)
126f1e39a4aSRalf Baechle 		: [addr] "r" (addr),
127f1e39a4aSRalf Baechle 		  [new] "r" (new),
128f1e39a4aSRalf Baechle 		  [efault] "i" (-EFAULT)
129f1e39a4aSRalf Baechle 		: "memory");
130f1e39a4aSRalf Baechle 	} else if (cpu_has_llsc) {
131f1e39a4aSRalf Baechle 		__asm__ __volatile__ (
132fee313d4SMarkos Chandras 		"	.set	"MIPS_ISA_ARCH_LEVEL"			\n"
133f1e39a4aSRalf Baechle 		"	li	%[err], 0				\n"
134f1e39a4aSRalf Baechle 		"1:	ll	%[old], (%[addr])			\n"
135f1e39a4aSRalf Baechle 		"	move	%[tmp], %[new]				\n"
136f1e39a4aSRalf Baechle 		"2:	sc	%[tmp], (%[addr])			\n"
1372ec420b2SJames Hogan 		"	beqz	%[tmp], 4f				\n"
138f1e39a4aSRalf Baechle 		"3:							\n"
1390e525e48SMaciej W. Rozycki 		"	.insn						\n"
140f1e39a4aSRalf Baechle 		"	.subsection 2					\n"
141f1e39a4aSRalf Baechle 		"4:	b	1b					\n"
142f1e39a4aSRalf Baechle 		"	.previous					\n"
143f1e39a4aSRalf Baechle 		"							\n"
144f1e39a4aSRalf Baechle 		"	.section .fixup,\"ax\"				\n"
145f1e39a4aSRalf Baechle 		"5:	li	%[err], %[efault]			\n"
146f1e39a4aSRalf Baechle 		"	j	3b					\n"
147f1e39a4aSRalf Baechle 		"	.previous					\n"
148f1e39a4aSRalf Baechle 		"	.section __ex_table,\"a\"			\n"
149f1e39a4aSRalf Baechle 		"	"STR(PTR)"	1b, 5b				\n"
150f1e39a4aSRalf Baechle 		"	"STR(PTR)"	2b, 5b				\n"
151f1e39a4aSRalf Baechle 		"	.previous					\n"
152a91be9eeSRalf Baechle 		"	.set	mips0					\n"
153f1e39a4aSRalf Baechle 		: [old] "=&r" (old),
154f1e39a4aSRalf Baechle 		  [err] "=&r" (err),
155f1e39a4aSRalf Baechle 		  [tmp] "=&r" (tmp)
156f1e39a4aSRalf Baechle 		: [addr] "r" (addr),
157f1e39a4aSRalf Baechle 		  [new] "r" (new),
158f1e39a4aSRalf Baechle 		  [efault] "i" (-EFAULT)
159f1e39a4aSRalf Baechle 		: "memory");
160f1e39a4aSRalf Baechle 	} else {
161f1e39a4aSRalf Baechle 		do {
162f1e39a4aSRalf Baechle 			preempt_disable();
163f1e39a4aSRalf Baechle 			ll_bit = 1;
164f1e39a4aSRalf Baechle 			ll_task = current;
165f1e39a4aSRalf Baechle 			preempt_enable();
166f1e39a4aSRalf Baechle 
167f1e39a4aSRalf Baechle 			err = __get_user(old, (unsigned int *) addr);
168f1e39a4aSRalf Baechle 			err |= __put_user(new, (unsigned int *) addr);
169f1e39a4aSRalf Baechle 			if (err)
170f1e39a4aSRalf Baechle 				break;
171f1e39a4aSRalf Baechle 			rmb();
172f1e39a4aSRalf Baechle 		} while (!ll_bit);
173f1e39a4aSRalf Baechle 	}
174f1e39a4aSRalf Baechle 
175f1e39a4aSRalf Baechle 	if (unlikely(err))
176f1e39a4aSRalf Baechle 		return err;
177f1e39a4aSRalf Baechle 
17812890d0fSRalf Baechle 	regs = current_pt_regs();
179f1e39a4aSRalf Baechle 	regs->regs[2] = old;
180f1e39a4aSRalf Baechle 	regs->regs[7] = 0;	/* No error */
181f1e39a4aSRalf Baechle 
182f1e39a4aSRalf Baechle 	/*
183f1e39a4aSRalf Baechle 	 * Don't let your children do this ...
184f1e39a4aSRalf Baechle 	 */
185f1e39a4aSRalf Baechle 	__asm__ __volatile__(
186f1e39a4aSRalf Baechle 	"	move	$29, %0						\n"
187f1e39a4aSRalf Baechle 	"	j	syscall_exit					\n"
188f1e39a4aSRalf Baechle 	: /* no outputs */
189f1e39a4aSRalf Baechle 	: "r" (regs));
190f1e39a4aSRalf Baechle 
191f1e39a4aSRalf Baechle 	/* unreached.  Honestly.  */
192f2ace931SRalf Baechle 	unreachable();
193f1e39a4aSRalf Baechle }
194f1e39a4aSRalf Baechle 
195*49955d84SJames Hogan /*
196*49955d84SJames Hogan  * mips_atomic_set() normally returns directly via syscall_exit potentially
197*49955d84SJames Hogan  * clobbering static registers, so be sure to preserve them.
198*49955d84SJames Hogan  */
199*49955d84SJames Hogan save_static_function(sys_sysmips);
200*49955d84SJames Hogan 
20112890d0fSRalf Baechle SYSCALL_DEFINE3(sysmips, long, cmd, long, arg1, long, arg2)
202f1e39a4aSRalf Baechle {
2031da177e4SLinus Torvalds 	switch (cmd) {
2041da177e4SLinus Torvalds 	case MIPS_ATOMIC_SET:
20512890d0fSRalf Baechle 		return mips_atomic_set(arg1, arg2);
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	case MIPS_FIXADE:
208293c5bd1SRalf Baechle 		if (arg1 & ~3)
209293c5bd1SRalf Baechle 			return -EINVAL;
210293c5bd1SRalf Baechle 
211293c5bd1SRalf Baechle 		if (arg1 & 1)
212293c5bd1SRalf Baechle 			set_thread_flag(TIF_FIXADE);
213293c5bd1SRalf Baechle 		else
214293c5bd1SRalf Baechle 			clear_thread_flag(TIF_FIXADE);
215293c5bd1SRalf Baechle 		if (arg1 & 2)
216293c5bd1SRalf Baechle 			set_thread_flag(TIF_LOGADE);
217293c5bd1SRalf Baechle 		else
218e56293b1SStefan Oberhumer 			clear_thread_flag(TIF_LOGADE);
219293c5bd1SRalf Baechle 
2201da177e4SLinus Torvalds 		return 0;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	case FLUSH_CACHE:
2231da177e4SLinus Torvalds 		__flush_cache_all();
2241da177e4SLinus Torvalds 		return 0;
2251da177e4SLinus Torvalds 	}
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	return -EINVAL;
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds /*
2311da177e4SLinus Torvalds  * No implemented yet ...
2321da177e4SLinus Torvalds  */
233dbda6ac0SRalf Baechle SYSCALL_DEFINE3(cachectl, char *, addr, int, nbytes, int, op)
2341da177e4SLinus Torvalds {
2351da177e4SLinus Torvalds 	return -ENOSYS;
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds /*
2391da177e4SLinus Torvalds  * If we ever come here the user sp is bad.  Zap the process right away.
2401da177e4SLinus Torvalds  * Due to the bad stack signaling wouldn't work.
2411da177e4SLinus Torvalds  */
2421da177e4SLinus Torvalds asmlinkage void bad_stack(void)
2431da177e4SLinus Torvalds {
2441da177e4SLinus Torvalds 	do_exit(SIGSEGV);
2451da177e4SLinus Torvalds }
246