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