xref: /openbmc/linux/arch/mips/kernel/syscall.c (revision 64c70b1c)
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/a.out.h>
11 #include <linux/capability.h>
12 #include <linux/errno.h>
13 #include <linux/linkage.h>
14 #include <linux/mm.h>
15 #include <linux/smp.h>
16 #include <linux/mman.h>
17 #include <linux/ptrace.h>
18 #include <linux/sched.h>
19 #include <linux/string.h>
20 #include <linux/syscalls.h>
21 #include <linux/file.h>
22 #include <linux/slab.h>
23 #include <linux/utsname.h>
24 #include <linux/unistd.h>
25 #include <linux/sem.h>
26 #include <linux/msg.h>
27 #include <linux/shm.h>
28 #include <linux/compiler.h>
29 #include <linux/module.h>
30 
31 #include <asm/branch.h>
32 #include <asm/cachectl.h>
33 #include <asm/cacheflush.h>
34 #include <asm/ipc.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 
42 asmlinkage int sys_pipe(nabi_no_regargs volatile struct pt_regs regs)
43 {
44 	int fd[2];
45 	int error, res;
46 
47 	error = do_pipe(fd);
48 	if (error) {
49 		res = error;
50 		goto out;
51 	}
52 	regs.regs[3] = fd[1];
53 	res = fd[0];
54 out:
55 	return res;
56 }
57 
58 unsigned long shm_align_mask = PAGE_SIZE - 1;	/* Sane caches */
59 
60 EXPORT_SYMBOL(shm_align_mask);
61 
62 #define COLOUR_ALIGN(addr,pgoff)				\
63 	((((addr) + shm_align_mask) & ~shm_align_mask) +	\
64 	 (((pgoff) << PAGE_SHIFT) & shm_align_mask))
65 
66 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
67 	unsigned long len, unsigned long pgoff, unsigned long flags)
68 {
69 	struct vm_area_struct * vmm;
70 	int do_color_align;
71 	unsigned long task_size;
72 
73 	task_size = STACK_TOP;
74 
75 	if (flags & MAP_FIXED) {
76 		/*
77 		 * We do not accept a shared mapping if it would violate
78 		 * cache aliasing constraints.
79 		 */
80 		if ((flags & MAP_SHARED) && (addr & shm_align_mask))
81 			return -EINVAL;
82 		return addr;
83 	}
84 
85 	if (len > task_size)
86 		return -ENOMEM;
87 	do_color_align = 0;
88 	if (filp || (flags & MAP_SHARED))
89 		do_color_align = 1;
90 	if (addr) {
91 		if (do_color_align)
92 			addr = COLOUR_ALIGN(addr, pgoff);
93 		else
94 			addr = PAGE_ALIGN(addr);
95 		vmm = find_vma(current->mm, addr);
96 		if (task_size - len >= addr &&
97 		    (!vmm || addr + len <= vmm->vm_start))
98 			return addr;
99 	}
100 	addr = TASK_UNMAPPED_BASE;
101 	if (do_color_align)
102 		addr = COLOUR_ALIGN(addr, pgoff);
103 	else
104 		addr = PAGE_ALIGN(addr);
105 
106 	for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
107 		/* At this point:  (!vmm || addr < vmm->vm_end). */
108 		if (task_size - len < addr)
109 			return -ENOMEM;
110 		if (!vmm || addr + len <= vmm->vm_start)
111 			return addr;
112 		addr = vmm->vm_end;
113 		if (do_color_align)
114 			addr = COLOUR_ALIGN(addr, pgoff);
115 	}
116 }
117 
118 /* common code for old and new mmaps */
119 static inline unsigned long
120 do_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
121         unsigned long flags, unsigned long fd, unsigned long pgoff)
122 {
123 	unsigned long error = -EBADF;
124 	struct file * file = NULL;
125 
126 	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
127 	if (!(flags & MAP_ANONYMOUS)) {
128 		file = fget(fd);
129 		if (!file)
130 			goto out;
131 	}
132 
133 	down_write(&current->mm->mmap_sem);
134 	error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
135 	up_write(&current->mm->mmap_sem);
136 
137 	if (file)
138 		fput(file);
139 out:
140 	return error;
141 }
142 
143 asmlinkage unsigned long
144 old_mmap(unsigned long addr, unsigned long len, int prot,
145 	int flags, int fd, off_t offset)
146 {
147 	unsigned long result;
148 
149 	result = -EINVAL;
150 	if (offset & ~PAGE_MASK)
151 		goto out;
152 
153 	result = do_mmap2(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
154 
155 out:
156 	return result;
157 }
158 
159 asmlinkage unsigned long
160 sys_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
161           unsigned long flags, unsigned long fd, unsigned long pgoff)
162 {
163 	if (pgoff & (~PAGE_MASK >> 12))
164 		return -EINVAL;
165 
166 	return do_mmap2(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT-12));
167 }
168 
169 save_static_function(sys_fork);
170 __attribute_used__ noinline static int
171 _sys_fork(nabi_no_regargs struct pt_regs regs)
172 {
173 	return do_fork(SIGCHLD, regs.regs[29], &regs, 0, NULL, NULL);
174 }
175 
176 save_static_function(sys_clone);
177 __attribute_used__ noinline static int
178 _sys_clone(nabi_no_regargs struct pt_regs regs)
179 {
180 	unsigned long clone_flags;
181 	unsigned long newsp;
182 	int __user *parent_tidptr, *child_tidptr;
183 
184 	clone_flags = regs.regs[4];
185 	newsp = regs.regs[5];
186 	if (!newsp)
187 		newsp = regs.regs[29];
188 	parent_tidptr = (int __user *) regs.regs[6];
189 #ifdef CONFIG_32BIT
190 	/* We need to fetch the fifth argument off the stack.  */
191 	child_tidptr = NULL;
192 	if (clone_flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) {
193 		int __user *__user *usp = (int __user *__user *) regs.regs[29];
194 		if (regs.regs[2] == __NR_syscall) {
195 			if (get_user (child_tidptr, &usp[5]))
196 				return -EFAULT;
197 		}
198 		else if (get_user (child_tidptr, &usp[4]))
199 			return -EFAULT;
200 	}
201 #else
202 	child_tidptr = (int __user *) regs.regs[8];
203 #endif
204 	return do_fork(clone_flags, newsp, &regs, 0,
205 	               parent_tidptr, child_tidptr);
206 }
207 
208 /*
209  * sys_execve() executes a new program.
210  */
211 asmlinkage int sys_execve(nabi_no_regargs struct pt_regs regs)
212 {
213 	int error;
214 	char * filename;
215 
216 	filename = getname((char __user *) (long)regs.regs[4]);
217 	error = PTR_ERR(filename);
218 	if (IS_ERR(filename))
219 		goto out;
220 	error = do_execve(filename, (char __user *__user *) (long)regs.regs[5],
221 	                  (char __user *__user *) (long)regs.regs[6], &regs);
222 	putname(filename);
223 
224 out:
225 	return error;
226 }
227 
228 /*
229  * Compacrapability ...
230  */
231 asmlinkage int sys_uname(struct old_utsname __user * name)
232 {
233 	if (name && !copy_to_user(name, utsname(), sizeof (*name)))
234 		return 0;
235 	return -EFAULT;
236 }
237 
238 /*
239  * Compacrapability ...
240  */
241 asmlinkage int sys_olduname(struct oldold_utsname __user * name)
242 {
243 	int error;
244 
245 	if (!name)
246 		return -EFAULT;
247 	if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
248 		return -EFAULT;
249 
250 	error = __copy_to_user(&name->sysname, &utsname()->sysname,
251 			       __OLD_UTS_LEN);
252 	error -= __put_user(0, name->sysname + __OLD_UTS_LEN);
253 	error -= __copy_to_user(&name->nodename, &utsname()->nodename,
254 				__OLD_UTS_LEN);
255 	error -= __put_user(0, name->nodename + __OLD_UTS_LEN);
256 	error -= __copy_to_user(&name->release, &utsname()->release,
257 				__OLD_UTS_LEN);
258 	error -= __put_user(0, name->release + __OLD_UTS_LEN);
259 	error -= __copy_to_user(&name->version, &utsname()->version,
260 				__OLD_UTS_LEN);
261 	error -= __put_user(0, name->version + __OLD_UTS_LEN);
262 	error -= __copy_to_user(&name->machine, &utsname()->machine,
263 				__OLD_UTS_LEN);
264 	error = __put_user(0, name->machine + __OLD_UTS_LEN);
265 	error = error ? -EFAULT : 0;
266 
267 	return error;
268 }
269 
270 asmlinkage int sys_set_thread_area(unsigned long addr)
271 {
272 	struct thread_info *ti = task_thread_info(current);
273 
274 	ti->tp_value = addr;
275 	if (cpu_has_userlocal)
276 		write_c0_userlocal(addr);
277 
278 	return 0;
279 }
280 
281 asmlinkage int _sys_sysmips(int cmd, long arg1, int arg2, int arg3)
282 {
283 	int	tmp;
284 
285 	switch(cmd) {
286 	case MIPS_ATOMIC_SET:
287 		printk(KERN_CRIT "How did I get here?\n");
288 		return -EINVAL;
289 
290 	case MIPS_FIXADE:
291 		tmp = current->thread.mflags & ~3;
292 		current->thread.mflags = tmp | (arg1 & 3);
293 		return 0;
294 
295 	case FLUSH_CACHE:
296 		__flush_cache_all();
297 		return 0;
298 	}
299 
300 	return -EINVAL;
301 }
302 
303 /*
304  * sys_ipc() is the de-multiplexer for the SysV IPC calls..
305  *
306  * This is really horribly ugly.
307  */
308 asmlinkage int sys_ipc (unsigned int call, int first, int second,
309 			unsigned long third, void __user *ptr, long fifth)
310 {
311 	int version, ret;
312 
313 	version = call >> 16; /* hack for backward compatibility */
314 	call &= 0xffff;
315 
316 	switch (call) {
317 	case SEMOP:
318 		return sys_semtimedop (first, (struct sembuf __user *)ptr,
319 		                       second, NULL);
320 	case SEMTIMEDOP:
321 		return sys_semtimedop (first, (struct sembuf __user *)ptr,
322 				       second,
323 				       (const struct timespec __user *)fifth);
324 	case SEMGET:
325 		return sys_semget (first, second, third);
326 	case SEMCTL: {
327 		union semun fourth;
328 		if (!ptr)
329 			return -EINVAL;
330 		if (get_user(fourth.__pad, (void __user *__user *) ptr))
331 			return -EFAULT;
332 		return sys_semctl (first, second, third, fourth);
333 	}
334 
335 	case MSGSND:
336 		return sys_msgsnd (first, (struct msgbuf __user *) ptr,
337 				   second, third);
338 	case MSGRCV:
339 		switch (version) {
340 		case 0: {
341 			struct ipc_kludge tmp;
342 			if (!ptr)
343 				return -EINVAL;
344 
345 			if (copy_from_user(&tmp,
346 					   (struct ipc_kludge __user *) ptr,
347 					   sizeof (tmp)))
348 				return -EFAULT;
349 			return sys_msgrcv (first, tmp.msgp, second,
350 					   tmp.msgtyp, third);
351 		}
352 		default:
353 			return sys_msgrcv (first,
354 					   (struct msgbuf __user *) ptr,
355 					   second, fifth, third);
356 		}
357 	case MSGGET:
358 		return sys_msgget ((key_t) first, second);
359 	case MSGCTL:
360 		return sys_msgctl (first, second,
361 				   (struct msqid_ds __user *) ptr);
362 
363 	case SHMAT:
364 		switch (version) {
365 		default: {
366 			unsigned long raddr;
367 			ret = do_shmat (first, (char __user *) ptr, second,
368 					&raddr);
369 			if (ret)
370 				return ret;
371 			return put_user (raddr, (unsigned long __user *) third);
372 		}
373 		case 1:	/* iBCS2 emulator entry point */
374 			if (!segment_eq(get_fs(), get_ds()))
375 				return -EINVAL;
376 			return do_shmat (first, (char __user *) ptr, second,
377 					 (unsigned long *) third);
378 		}
379 	case SHMDT:
380 		return sys_shmdt ((char __user *)ptr);
381 	case SHMGET:
382 		return sys_shmget (first, second, third);
383 	case SHMCTL:
384 		return sys_shmctl (first, second,
385 				   (struct shmid_ds __user *) ptr);
386 	default:
387 		return -ENOSYS;
388 	}
389 }
390 
391 /*
392  * No implemented yet ...
393  */
394 asmlinkage int sys_cachectl(char *addr, int nbytes, int op)
395 {
396 	return -ENOSYS;
397 }
398 
399 /*
400  * If we ever come here the user sp is bad.  Zap the process right away.
401  * Due to the bad stack signaling wouldn't work.
402  */
403 asmlinkage void bad_stack(void)
404 {
405 	do_exit(SIGSEGV);
406 }
407 
408 /*
409  * Do a system call from kernel instead of calling sys_execve so we
410  * end up with proper pt_regs.
411  */
412 int kernel_execve(const char *filename, char *const argv[], char *const envp[])
413 {
414 	register unsigned long __a0 asm("$4") = (unsigned long) filename;
415 	register unsigned long __a1 asm("$5") = (unsigned long) argv;
416 	register unsigned long __a2 asm("$6") = (unsigned long) envp;
417 	register unsigned long __a3 asm("$7");
418 	unsigned long __v0;
419 
420 	__asm__ volatile ("					\n"
421 	"	.set	noreorder				\n"
422 	"	li	$2, %5		# __NR_execve		\n"
423 	"	syscall						\n"
424 	"	move	%0, $2					\n"
425 	"	.set	reorder					\n"
426 	: "=&r" (__v0), "=r" (__a3)
427 	: "r" (__a0), "r" (__a1), "r" (__a2), "i" (__NR_execve)
428 	: "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24",
429 	  "memory");
430 
431 	if (__a3 == 0)
432 		return __v0;
433 
434 	return -__v0;
435 }
436