GNU Linux-libre 4.4.289-gnu1
[releases.git] / arch / mips / kernel / syscall.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1995, 1996, 1997, 2000, 2001, 05 by Ralf Baechle
7  * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
8  * Copyright (C) 2001 MIPS Technologies, Inc.
9  */
10 #include <linux/capability.h>
11 #include <linux/errno.h>
12 #include <linux/linkage.h>
13 #include <linux/fs.h>
14 #include <linux/smp.h>
15 #include <linux/ptrace.h>
16 #include <linux/string.h>
17 #include <linux/syscalls.h>
18 #include <linux/file.h>
19 #include <linux/utsname.h>
20 #include <linux/unistd.h>
21 #include <linux/sem.h>
22 #include <linux/msg.h>
23 #include <linux/shm.h>
24 #include <linux/compiler.h>
25 #include <linux/ipc.h>
26 #include <linux/uaccess.h>
27 #include <linux/slab.h>
28 #include <linux/elf.h>
29
30 #include <asm/asm.h>
31 #include <asm/asm-eva.h>
32 #include <asm/branch.h>
33 #include <asm/cachectl.h>
34 #include <asm/cacheflush.h>
35 #include <asm/asm-offsets.h>
36 #include <asm/signal.h>
37 #include <asm/sim.h>
38 #include <asm/shmparam.h>
39 #include <asm/sysmips.h>
40 #include <asm/uaccess.h>
41 #include <asm/switch_to.h>
42
43 /*
44  * For historic reasons the pipe(2) syscall on MIPS has an unusual calling
45  * convention.  It returns results in registers $v0 / $v1 which means there
46  * is no need for it to do verify the validity of a userspace pointer
47  * argument.  Historically that used to be expensive in Linux.  These days
48  * the performance advantage is negligible.
49  */
50 asmlinkage int sysm_pipe(void)
51 {
52         int fd[2];
53         int error = do_pipe_flags(fd, 0);
54         if (error)
55                 return error;
56         current_pt_regs()->regs[3] = fd[1];
57         return fd[0];
58 }
59
60 SYSCALL_DEFINE6(mips_mmap, unsigned long, addr, unsigned long, len,
61         unsigned long, prot, unsigned long, flags, unsigned long,
62         fd, off_t, offset)
63 {
64         unsigned long result;
65
66         result = -EINVAL;
67         if (offset & ~PAGE_MASK)
68                 goto out;
69
70         result = sys_mmap_pgoff(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
71
72 out:
73         return result;
74 }
75
76 SYSCALL_DEFINE6(mips_mmap2, unsigned long, addr, unsigned long, len,
77         unsigned long, prot, unsigned long, flags, unsigned long, fd,
78         unsigned long, pgoff)
79 {
80         if (pgoff & (~PAGE_MASK >> 12))
81                 return -EINVAL;
82
83         return sys_mmap_pgoff(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT-12));
84 }
85
86 save_static_function(sys_fork);
87 save_static_function(sys_clone);
88
89 SYSCALL_DEFINE1(set_thread_area, unsigned long, addr)
90 {
91         struct thread_info *ti = task_thread_info(current);
92
93         ti->tp_value = addr;
94         if (cpu_has_userlocal)
95                 write_c0_userlocal(addr);
96
97         return 0;
98 }
99
100 static inline int mips_atomic_set(unsigned long addr, unsigned long new)
101 {
102         unsigned long old, tmp;
103         struct pt_regs *regs;
104         unsigned int err;
105
106         if (unlikely(addr & 3))
107                 return -EINVAL;
108
109         if (unlikely(!access_ok(VERIFY_WRITE, addr, 4)))
110                 return -EINVAL;
111
112         if (cpu_has_llsc && R10000_LLSC_WAR) {
113                 __asm__ __volatile__ (
114                 "       .set    arch=r4000                              \n"
115                 "       li      %[err], 0                               \n"
116                 "1:     ll      %[old], (%[addr])                       \n"
117                 "       move    %[tmp], %[new]                          \n"
118                 "2:     sc      %[tmp], (%[addr])                       \n"
119                 "       beqzl   %[tmp], 1b                              \n"
120                 "3:                                                     \n"
121                 "       .insn                                           \n"
122                 "       .section .fixup,\"ax\"                          \n"
123                 "4:     li      %[err], %[efault]                       \n"
124                 "       j       3b                                      \n"
125                 "       .previous                                       \n"
126                 "       .section __ex_table,\"a\"                       \n"
127                 "       "STR(PTR)"      1b, 4b                          \n"
128                 "       "STR(PTR)"      2b, 4b                          \n"
129                 "       .previous                                       \n"
130                 "       .set    mips0                                   \n"
131                 : [old] "=&r" (old),
132                   [err] "=&r" (err),
133                   [tmp] "=&r" (tmp)
134                 : [addr] "r" (addr),
135                   [new] "r" (new),
136                   [efault] "i" (-EFAULT)
137                 : "memory");
138         } else if (cpu_has_llsc) {
139                 __asm__ __volatile__ (
140                 "       .set    "MIPS_ISA_ARCH_LEVEL"                   \n"
141                 "       li      %[err], 0                               \n"
142                 "1:                                                     \n"
143                 user_ll("%[old]", "(%[addr])")
144                 "       move    %[tmp], %[new]                          \n"
145                 "2:                                                     \n"
146                 user_sc("%[tmp]", "(%[addr])")
147                 "       beqz    %[tmp], 4f                              \n"
148                 "3:                                                     \n"
149                 "       .insn                                           \n"
150                 "       .subsection 2                                   \n"
151                 "4:     b       1b                                      \n"
152                 "       .previous                                       \n"
153                 "                                                       \n"
154                 "       .section .fixup,\"ax\"                          \n"
155                 "5:     li      %[err], %[efault]                       \n"
156                 "       j       3b                                      \n"
157                 "       .previous                                       \n"
158                 "       .section __ex_table,\"a\"                       \n"
159                 "       "STR(PTR)"      1b, 5b                          \n"
160                 "       "STR(PTR)"      2b, 5b                          \n"
161                 "       .previous                                       \n"
162                 "       .set    mips0                                   \n"
163                 : [old] "=&r" (old),
164                   [err] "=&r" (err),
165                   [tmp] "=&r" (tmp)
166                 : [addr] "r" (addr),
167                   [new] "r" (new),
168                   [efault] "i" (-EFAULT)
169                 : "memory");
170         } else {
171                 do {
172                         preempt_disable();
173                         ll_bit = 1;
174                         ll_task = current;
175                         preempt_enable();
176
177                         err = __get_user(old, (unsigned int *) addr);
178                         err |= __put_user(new, (unsigned int *) addr);
179                         if (err)
180                                 break;
181                         rmb();
182                 } while (!ll_bit);
183         }
184
185         if (unlikely(err))
186                 return err;
187
188         regs = current_pt_regs();
189         regs->regs[2] = old;
190         regs->regs[7] = 0;      /* No error */
191
192         /*
193          * Don't let your children do this ...
194          */
195         __asm__ __volatile__(
196         "       move    $29, %0                                         \n"
197         "       j       syscall_exit                                    \n"
198         : /* no outputs */
199         : "r" (regs));
200
201         /* unreached.  Honestly.  */
202         unreachable();
203 }
204
205 /*
206  * mips_atomic_set() normally returns directly via syscall_exit potentially
207  * clobbering static registers, so be sure to preserve them.
208  */
209 save_static_function(sys_sysmips);
210
211 SYSCALL_DEFINE3(sysmips, long, cmd, long, arg1, long, arg2)
212 {
213         switch (cmd) {
214         case MIPS_ATOMIC_SET:
215                 return mips_atomic_set(arg1, arg2);
216
217         case MIPS_FIXADE:
218                 if (arg1 & ~3)
219                         return -EINVAL;
220
221                 if (arg1 & 1)
222                         set_thread_flag(TIF_FIXADE);
223                 else
224                         clear_thread_flag(TIF_FIXADE);
225                 if (arg1 & 2)
226                         set_thread_flag(TIF_LOGADE);
227                 else
228                         clear_thread_flag(TIF_LOGADE);
229
230                 return 0;
231
232         case FLUSH_CACHE:
233                 __flush_cache_all();
234                 return 0;
235         }
236
237         return -EINVAL;
238 }
239
240 /*
241  * No implemented yet ...
242  */
243 SYSCALL_DEFINE3(cachectl, char *, addr, int, nbytes, int, op)
244 {
245         return -ENOSYS;
246 }
247
248 /*
249  * If we ever come here the user sp is bad.  Zap the process right away.
250  * Due to the bad stack signaling wouldn't work.
251  */
252 asmlinkage void bad_stack(void)
253 {
254         do_exit(SIGSEGV);
255 }