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>
324915e1b0SJames Hogan #include <asm/asm-eva.h>
331da177e4SLinus Torvalds #include <asm/branch.h>
341da177e4SLinus Torvalds #include <asm/cachectl.h>
351da177e4SLinus Torvalds #include <asm/cacheflush.h>
36048eb582SSam Ravnborg #include <asm/asm-offsets.h>
371da177e4SLinus Torvalds #include <asm/signal.h>
381da177e4SLinus Torvalds #include <asm/sim.h>
391da177e4SLinus Torvalds #include <asm/shmparam.h>
40e84957e6SPaul Burton #include <asm/sync.h>
411da177e4SLinus Torvalds #include <asm/sysmips.h>
42b81947c6SDavid Howells #include <asm/switch_to.h>
431da177e4SLinus Torvalds
448213bbf9SRalf Baechle /*
458213bbf9SRalf Baechle * For historic reasons the pipe(2) syscall on MIPS has an unusual calling
468213bbf9SRalf Baechle * convention. It returns results in registers $v0 / $v1 which means there
478213bbf9SRalf Baechle * is no need for it to do verify the validity of a userspace pointer
488213bbf9SRalf Baechle * argument. Historically that used to be expensive in Linux. These days
498213bbf9SRalf Baechle * the performance advantage is negligible.
508213bbf9SRalf Baechle */
sysm_pipe(void)51974fdb3cSAl Viro asmlinkage int sysm_pipe(void)
521da177e4SLinus Torvalds {
531da177e4SLinus Torvalds int fd[2];
54974fdb3cSAl Viro int error = do_pipe_flags(fd, 0);
55974fdb3cSAl Viro if (error)
56974fdb3cSAl Viro return error;
57974fdb3cSAl Viro current_pt_regs()->regs[3] = fd[1];
58974fdb3cSAl Viro return fd[0];
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds
SYSCALL_DEFINE6(mips_mmap,unsigned long,addr,unsigned long,len,unsigned long,prot,unsigned long,flags,unsigned long,fd,off_t,offset)61dbda6ac0SRalf Baechle SYSCALL_DEFINE6(mips_mmap, unsigned long, addr, unsigned long, len,
62dbda6ac0SRalf Baechle unsigned long, prot, unsigned long, flags, unsigned long,
63dbda6ac0SRalf Baechle fd, off_t, offset)
641da177e4SLinus Torvalds {
651da177e4SLinus Torvalds if (offset & ~PAGE_MASK)
66c9d3fdf3SMarkus Elfring return -EINVAL;
67a90f590aSDominik Brodowski return ksys_mmap_pgoff(addr, len, prot, flags, fd,
68a90f590aSDominik Brodowski offset >> PAGE_SHIFT);
691da177e4SLinus Torvalds }
701da177e4SLinus Torvalds
SYSCALL_DEFINE6(mips_mmap2,unsigned long,addr,unsigned long,len,unsigned long,prot,unsigned long,flags,unsigned long,fd,unsigned long,pgoff)71dbda6ac0SRalf Baechle SYSCALL_DEFINE6(mips_mmap2, unsigned long, addr, unsigned long, len,
72dbda6ac0SRalf Baechle unsigned long, prot, unsigned long, flags, unsigned long, fd,
73dbda6ac0SRalf Baechle unsigned long, pgoff)
741da177e4SLinus Torvalds {
75947df17cSH. Peter Anvin if (pgoff & (~PAGE_MASK >> 12))
76947df17cSH. Peter Anvin return -EINVAL;
77947df17cSH. Peter Anvin
78a90f590aSDominik Brodowski return ksys_mmap_pgoff(addr, len, prot, flags, fd,
79a90f590aSDominik Brodowski pgoff >> (PAGE_SHIFT - 12));
801da177e4SLinus Torvalds }
811da177e4SLinus Torvalds
821da177e4SLinus Torvalds save_static_function(sys_fork);
831da177e4SLinus Torvalds save_static_function(sys_clone);
840671c5b8SPaul Burton save_static_function(sys_clone3);
851da177e4SLinus Torvalds
SYSCALL_DEFINE1(set_thread_area,unsigned long,addr)86dbda6ac0SRalf Baechle SYSCALL_DEFINE1(set_thread_area, unsigned long, addr)
873c37026dSRalf Baechle {
88dc8f6029SAl Viro struct thread_info *ti = task_thread_info(current);
893c37026dSRalf Baechle
903c37026dSRalf Baechle ti->tp_value = addr;
91a3692020SRalf Baechle if (cpu_has_userlocal)
92a3692020SRalf Baechle write_c0_userlocal(addr);
9306be375bSRalf Baechle
9406be375bSRalf Baechle return 0;
953c37026dSRalf Baechle }
963c37026dSRalf Baechle
mips_atomic_set(unsigned long addr,unsigned long new)9712890d0fSRalf Baechle static inline int mips_atomic_set(unsigned long addr, unsigned long new)
981da177e4SLinus Torvalds {
99f1e39a4aSRalf Baechle unsigned long old, tmp;
10012890d0fSRalf Baechle struct pt_regs *regs;
101f1e39a4aSRalf Baechle unsigned int err;
102f1e39a4aSRalf Baechle
103f1e39a4aSRalf Baechle if (unlikely(addr & 3))
104f1e39a4aSRalf Baechle return -EINVAL;
105f1e39a4aSRalf Baechle
10696d4f267SLinus Torvalds if (unlikely(!access_ok((const void __user *)addr, 4)))
107f1e39a4aSRalf Baechle return -EINVAL;
108f1e39a4aSRalf Baechle
109256ec489SThomas Bogendoerfer if (cpu_has_llsc && IS_ENABLED(CONFIG_WAR_R10000_LLSC)) {
110f1e39a4aSRalf Baechle __asm__ __volatile__ (
111378ed6f0SPaul Burton " .set push \n"
112a809d460SRalf Baechle " .set arch=r4000 \n"
113f1e39a4aSRalf Baechle " li %[err], 0 \n"
114f1e39a4aSRalf Baechle "1: ll %[old], (%[addr]) \n"
115f1e39a4aSRalf Baechle " move %[tmp], %[new] \n"
116f1e39a4aSRalf Baechle "2: sc %[tmp], (%[addr]) \n"
117f1e39a4aSRalf Baechle " beqzl %[tmp], 1b \n"
118f1e39a4aSRalf Baechle "3: \n"
1190e525e48SMaciej W. Rozycki " .insn \n"
120f1e39a4aSRalf Baechle " .section .fixup,\"ax\" \n"
121f1e39a4aSRalf Baechle "4: li %[err], %[efault] \n"
122f1e39a4aSRalf Baechle " j 3b \n"
123f1e39a4aSRalf Baechle " .previous \n"
124f1e39a4aSRalf Baechle " .section __ex_table,\"a\" \n"
125*fa62f39dSThomas Bogendoerfer " "STR(PTR_WD)" 1b, 4b \n"
126*fa62f39dSThomas Bogendoerfer " "STR(PTR_WD)" 2b, 4b \n"
127f1e39a4aSRalf Baechle " .previous \n"
128378ed6f0SPaul Burton " .set pop \n"
129f1e39a4aSRalf Baechle : [old] "=&r" (old),
130f1e39a4aSRalf Baechle [err] "=&r" (err),
131f1e39a4aSRalf Baechle [tmp] "=&r" (tmp)
132f1e39a4aSRalf Baechle : [addr] "r" (addr),
133f1e39a4aSRalf Baechle [new] "r" (new),
134f1e39a4aSRalf Baechle [efault] "i" (-EFAULT)
135f1e39a4aSRalf Baechle : "memory");
136f1e39a4aSRalf Baechle } else if (cpu_has_llsc) {
137f1e39a4aSRalf Baechle __asm__ __volatile__ (
138378ed6f0SPaul Burton " .set push \n"
139fee313d4SMarkos Chandras " .set "MIPS_ISA_ARCH_LEVEL" \n"
140f1e39a4aSRalf Baechle " li %[err], 0 \n"
1414915e1b0SJames Hogan "1: \n"
142e84957e6SPaul Burton " " __SYNC(full, loongson3_war) " \n"
1434915e1b0SJames Hogan user_ll("%[old]", "(%[addr])")
144f1e39a4aSRalf Baechle " move %[tmp], %[new] \n"
1454915e1b0SJames Hogan "2: \n"
1464915e1b0SJames Hogan user_sc("%[tmp]", "(%[addr])")
147203e090aSJames Hogan " beqz %[tmp], 1b \n"
148f1e39a4aSRalf Baechle "3: \n"
1490e525e48SMaciej W. Rozycki " .insn \n"
150f1e39a4aSRalf Baechle " .section .fixup,\"ax\" \n"
151f1e39a4aSRalf Baechle "5: li %[err], %[efault] \n"
152f1e39a4aSRalf Baechle " j 3b \n"
153f1e39a4aSRalf Baechle " .previous \n"
154f1e39a4aSRalf Baechle " .section __ex_table,\"a\" \n"
155*fa62f39dSThomas Bogendoerfer " "STR(PTR_WD)" 1b, 5b \n"
156*fa62f39dSThomas Bogendoerfer " "STR(PTR_WD)" 2b, 5b \n"
157f1e39a4aSRalf Baechle " .previous \n"
158378ed6f0SPaul Burton " .set pop \n"
159f1e39a4aSRalf Baechle : [old] "=&r" (old),
160f1e39a4aSRalf Baechle [err] "=&r" (err),
161f1e39a4aSRalf Baechle [tmp] "=&r" (tmp)
162f1e39a4aSRalf Baechle : [addr] "r" (addr),
163f1e39a4aSRalf Baechle [new] "r" (new),
164f1e39a4aSRalf Baechle [efault] "i" (-EFAULT)
165f1e39a4aSRalf Baechle : "memory");
166f1e39a4aSRalf Baechle } else {
167f1e39a4aSRalf Baechle do {
168f1e39a4aSRalf Baechle preempt_disable();
169f1e39a4aSRalf Baechle ll_bit = 1;
170f1e39a4aSRalf Baechle ll_task = current;
171f1e39a4aSRalf Baechle preempt_enable();
172f1e39a4aSRalf Baechle
173f1e39a4aSRalf Baechle err = __get_user(old, (unsigned int *) addr);
174f1e39a4aSRalf Baechle err |= __put_user(new, (unsigned int *) addr);
175f1e39a4aSRalf Baechle if (err)
176f1e39a4aSRalf Baechle break;
177f1e39a4aSRalf Baechle rmb();
178f1e39a4aSRalf Baechle } while (!ll_bit);
179f1e39a4aSRalf Baechle }
180f1e39a4aSRalf Baechle
181f1e39a4aSRalf Baechle if (unlikely(err))
182f1e39a4aSRalf Baechle return err;
183f1e39a4aSRalf Baechle
18412890d0fSRalf Baechle regs = current_pt_regs();
185f1e39a4aSRalf Baechle regs->regs[2] = old;
186f1e39a4aSRalf Baechle regs->regs[7] = 0; /* No error */
187f1e39a4aSRalf Baechle
188f1e39a4aSRalf Baechle /*
189f1e39a4aSRalf Baechle * Don't let your children do this ...
190f1e39a4aSRalf Baechle */
191f1e39a4aSRalf Baechle __asm__ __volatile__(
192f1e39a4aSRalf Baechle " move $29, %0 \n"
193f1e39a4aSRalf Baechle " j syscall_exit \n"
194f1e39a4aSRalf Baechle : /* no outputs */
195f1e39a4aSRalf Baechle : "r" (regs));
196f1e39a4aSRalf Baechle
197f1e39a4aSRalf Baechle /* unreached. Honestly. */
198f2ace931SRalf Baechle unreachable();
199f1e39a4aSRalf Baechle }
200f1e39a4aSRalf Baechle
20149955d84SJames Hogan /*
20249955d84SJames Hogan * mips_atomic_set() normally returns directly via syscall_exit potentially
20349955d84SJames Hogan * clobbering static registers, so be sure to preserve them.
20449955d84SJames Hogan */
20549955d84SJames Hogan save_static_function(sys_sysmips);
20649955d84SJames Hogan
SYSCALL_DEFINE3(sysmips,long,cmd,long,arg1,long,arg2)20712890d0fSRalf Baechle SYSCALL_DEFINE3(sysmips, long, cmd, long, arg1, long, arg2)
208f1e39a4aSRalf Baechle {
2091da177e4SLinus Torvalds switch (cmd) {
2101da177e4SLinus Torvalds case MIPS_ATOMIC_SET:
21112890d0fSRalf Baechle return mips_atomic_set(arg1, arg2);
2121da177e4SLinus Torvalds
2131da177e4SLinus Torvalds case MIPS_FIXADE:
214293c5bd1SRalf Baechle if (arg1 & ~3)
215293c5bd1SRalf Baechle return -EINVAL;
216293c5bd1SRalf Baechle
217293c5bd1SRalf Baechle if (arg1 & 1)
218293c5bd1SRalf Baechle set_thread_flag(TIF_FIXADE);
219293c5bd1SRalf Baechle else
220293c5bd1SRalf Baechle clear_thread_flag(TIF_FIXADE);
221293c5bd1SRalf Baechle if (arg1 & 2)
222293c5bd1SRalf Baechle set_thread_flag(TIF_LOGADE);
223293c5bd1SRalf Baechle else
224e56293b1SStefan Oberhumer clear_thread_flag(TIF_LOGADE);
225293c5bd1SRalf Baechle
2261da177e4SLinus Torvalds return 0;
2271da177e4SLinus Torvalds
2281da177e4SLinus Torvalds case FLUSH_CACHE:
2291da177e4SLinus Torvalds __flush_cache_all();
2301da177e4SLinus Torvalds return 0;
2311da177e4SLinus Torvalds }
2321da177e4SLinus Torvalds
2331da177e4SLinus Torvalds return -EINVAL;
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds
2361da177e4SLinus Torvalds /*
2371da177e4SLinus Torvalds * No implemented yet ...
2381da177e4SLinus Torvalds */
SYSCALL_DEFINE3(cachectl,char *,addr,int,nbytes,int,op)239dbda6ac0SRalf Baechle SYSCALL_DEFINE3(cachectl, char *, addr, int, nbytes, int, op)
2401da177e4SLinus Torvalds {
2411da177e4SLinus Torvalds return -ENOSYS;
2421da177e4SLinus Torvalds }
243