xref: /openbmc/linux/arch/sh/kernel/sys_sh.c (revision fa43972fab24a3c050e880a7831f9378c6cebc0b)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/arch/sh/kernel/sys_sh.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * This file contains various random system calls that
51da177e4SLinus Torvalds  * have a non-standard calling sequence on the Linux/SuperH
61da177e4SLinus Torvalds  * platform.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Taken from i386 version.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds #include <linux/errno.h>
111da177e4SLinus Torvalds #include <linux/sched.h>
121da177e4SLinus Torvalds #include <linux/mm.h>
131da177e4SLinus Torvalds #include <linux/smp.h>
141da177e4SLinus Torvalds #include <linux/sem.h>
151da177e4SLinus Torvalds #include <linux/msg.h>
161da177e4SLinus Torvalds #include <linux/shm.h>
171da177e4SLinus Torvalds #include <linux/stat.h>
181da177e4SLinus Torvalds #include <linux/syscalls.h>
191da177e4SLinus Torvalds #include <linux/mman.h>
201da177e4SLinus Torvalds #include <linux/file.h>
211da177e4SLinus Torvalds #include <linux/utsname.h>
22f3c25758SPaul Mundt #include <linux/module.h>
23e06c4e57SPaul Mundt #include <linux/fs.h>
24cba4fbbfSAdrian Bunk #include <linux/ipc.h>
2526ff6c11SPaul Mundt #include <asm/cacheflush.h>
26*fa43972fSPaul Mundt #include <asm/syscalls.h>
271da177e4SLinus Torvalds #include <asm/uaccess.h>
28fe74290dSArnd Bergmann #include <asm/unistd.h>
291da177e4SLinus Torvalds 
30f3c25758SPaul Mundt unsigned long shm_align_mask = PAGE_SIZE - 1;	/* Sane caches */
31f3c25758SPaul Mundt EXPORT_SYMBOL(shm_align_mask);
32f3c25758SPaul Mundt 
33710ee0ccSPaul Mundt #ifdef CONFIG_MMU
341da177e4SLinus Torvalds /*
35f3c25758SPaul Mundt  * To avoid cache aliases, we map the shared page with same color.
361da177e4SLinus Torvalds  */
37f3c25758SPaul Mundt #define COLOUR_ALIGN(addr, pgoff)				\
38f3c25758SPaul Mundt 	((((addr) + shm_align_mask) & ~shm_align_mask) +	\
39f3c25758SPaul Mundt 	 (((pgoff) << PAGE_SHIFT) & shm_align_mask))
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
421da177e4SLinus Torvalds 	unsigned long len, unsigned long pgoff, unsigned long flags)
431da177e4SLinus Torvalds {
441da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
451da177e4SLinus Torvalds 	struct vm_area_struct *vma;
461da177e4SLinus Torvalds 	unsigned long start_addr;
47f3c25758SPaul Mundt 	int do_colour_align;
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds 	if (flags & MAP_FIXED) {
501da177e4SLinus Torvalds 		/* We do not accept a shared mapping if it would violate
511da177e4SLinus Torvalds 		 * cache aliasing constraints.
521da177e4SLinus Torvalds 		 */
53f3c25758SPaul Mundt 		if ((flags & MAP_SHARED) && (addr & shm_align_mask))
541da177e4SLinus Torvalds 			return -EINVAL;
551da177e4SLinus Torvalds 		return addr;
561da177e4SLinus Torvalds 	}
571da177e4SLinus Torvalds 
58f3c25758SPaul Mundt 	if (unlikely(len > TASK_SIZE))
591da177e4SLinus Torvalds 		return -ENOMEM;
601da177e4SLinus Torvalds 
61f3c25758SPaul Mundt 	do_colour_align = 0;
62f3c25758SPaul Mundt 	if (filp || (flags & MAP_SHARED))
63f3c25758SPaul Mundt 		do_colour_align = 1;
64f3c25758SPaul Mundt 
651da177e4SLinus Torvalds 	if (addr) {
66f3c25758SPaul Mundt 		if (do_colour_align)
67f3c25758SPaul Mundt 			addr = COLOUR_ALIGN(addr, pgoff);
681da177e4SLinus Torvalds 		else
69f3c25758SPaul Mundt 			addr = PAGE_ALIGN(addr);
70f3c25758SPaul Mundt 
711da177e4SLinus Torvalds 		vma = find_vma(mm, addr);
721da177e4SLinus Torvalds 		if (TASK_SIZE - len >= addr &&
731da177e4SLinus Torvalds 		    (!vma || addr + len <= vma->vm_start))
741da177e4SLinus Torvalds 			return addr;
751da177e4SLinus Torvalds 	}
76f3c25758SPaul Mundt 
77f3c25758SPaul Mundt 	if (len > mm->cached_hole_size) {
78f3c25758SPaul Mundt 		start_addr = addr = mm->free_area_cache;
79f3c25758SPaul Mundt 	} else {
801363c3cdSWolfgang Wander 	        mm->cached_hole_size = 0;
81f3c25758SPaul Mundt 		start_addr = addr = TASK_UNMAPPED_BASE;
821363c3cdSWolfgang Wander 	}
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds full_search:
85f3c25758SPaul Mundt 	if (do_colour_align)
86f3c25758SPaul Mundt 		addr = COLOUR_ALIGN(addr, pgoff);
87f3c25758SPaul Mundt 	else
88f3c25758SPaul Mundt 		addr = PAGE_ALIGN(mm->free_area_cache);
89f3c25758SPaul Mundt 
901da177e4SLinus Torvalds 	for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
911da177e4SLinus Torvalds 		/* At this point:  (!vma || addr < vma->vm_end). */
92f3c25758SPaul Mundt 		if (unlikely(TASK_SIZE - len < addr)) {
931da177e4SLinus Torvalds 			/*
941da177e4SLinus Torvalds 			 * Start a new search - just in case we missed
951da177e4SLinus Torvalds 			 * some holes.
961da177e4SLinus Torvalds 			 */
971da177e4SLinus Torvalds 			if (start_addr != TASK_UNMAPPED_BASE) {
981da177e4SLinus Torvalds 				start_addr = addr = TASK_UNMAPPED_BASE;
991363c3cdSWolfgang Wander 				mm->cached_hole_size = 0;
1001da177e4SLinus Torvalds 				goto full_search;
1011da177e4SLinus Torvalds 			}
1021da177e4SLinus Torvalds 			return -ENOMEM;
1031da177e4SLinus Torvalds 		}
104f3c25758SPaul Mundt 		if (likely(!vma || addr + len <= vma->vm_start)) {
1051da177e4SLinus Torvalds 			/*
1061da177e4SLinus Torvalds 			 * Remember the place where we stopped the search:
1071da177e4SLinus Torvalds 			 */
1081da177e4SLinus Torvalds 			mm->free_area_cache = addr + len;
1091da177e4SLinus Torvalds 			return addr;
1101da177e4SLinus Torvalds 		}
1111363c3cdSWolfgang Wander 		if (addr + mm->cached_hole_size < vma->vm_start)
1121363c3cdSWolfgang Wander 		        mm->cached_hole_size = vma->vm_start - addr;
1131363c3cdSWolfgang Wander 
1141da177e4SLinus Torvalds 		addr = vma->vm_end;
115f3c25758SPaul Mundt 		if (do_colour_align)
116f3c25758SPaul Mundt 			addr = COLOUR_ALIGN(addr, pgoff);
1171da177e4SLinus Torvalds 	}
1181da177e4SLinus Torvalds }
119710ee0ccSPaul Mundt #endif /* CONFIG_MMU */
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds static inline long
1221da177e4SLinus Torvalds do_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
1231da177e4SLinus Torvalds 	 unsigned long flags, int fd, unsigned long pgoff)
1241da177e4SLinus Torvalds {
1251da177e4SLinus Torvalds 	int error = -EBADF;
1261da177e4SLinus Torvalds 	struct file *file = NULL;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1291da177e4SLinus Torvalds 	if (!(flags & MAP_ANONYMOUS)) {
1301da177e4SLinus Torvalds 		file = fget(fd);
1311da177e4SLinus Torvalds 		if (!file)
1321da177e4SLinus Torvalds 			goto out;
1331da177e4SLinus Torvalds 	}
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	down_write(&current->mm->mmap_sem);
1361da177e4SLinus Torvalds 	error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1371da177e4SLinus Torvalds 	up_write(&current->mm->mmap_sem);
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	if (file)
1401da177e4SLinus Torvalds 		fput(file);
1411da177e4SLinus Torvalds out:
1421da177e4SLinus Torvalds 	return error;
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds asmlinkage int old_mmap(unsigned long addr, unsigned long len,
1461da177e4SLinus Torvalds 	unsigned long prot, unsigned long flags,
1471da177e4SLinus Torvalds 	int fd, unsigned long off)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds 	if (off & ~PAGE_MASK)
1501da177e4SLinus Torvalds 		return -EINVAL;
1511da177e4SLinus Torvalds 	return do_mmap2(addr, len, prot, flags, fd, off>>PAGE_SHIFT);
1521da177e4SLinus Torvalds }
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds asmlinkage long sys_mmap2(unsigned long addr, unsigned long len,
1551da177e4SLinus Torvalds 	unsigned long prot, unsigned long flags,
1561da177e4SLinus Torvalds 	unsigned long fd, unsigned long pgoff)
1571da177e4SLinus Torvalds {
1581da177e4SLinus Torvalds 	return do_mmap2(addr, len, prot, flags, fd, pgoff);
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds /*
1621da177e4SLinus Torvalds  * sys_ipc() is the de-multiplexer for the SysV IPC calls..
1631da177e4SLinus Torvalds  *
1641da177e4SLinus Torvalds  * This is really horribly ugly.
1651da177e4SLinus Torvalds  */
1661da177e4SLinus Torvalds asmlinkage int sys_ipc(uint call, int first, int second,
1671da177e4SLinus Torvalds 		       int third, void __user *ptr, long fifth)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds 	int version, ret;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	version = call >> 16; /* hack for backward compatibility */
1721da177e4SLinus Torvalds 	call &= 0xffff;
1731da177e4SLinus Torvalds 
1743c31bf73SYoshihiro Shimoda 	if (call <= SEMTIMEDOP)
1751da177e4SLinus Torvalds 		switch (call) {
1761da177e4SLinus Torvalds 		case SEMOP:
177bcb28e42SPaul Mundt 			return sys_semtimedop(first,
178bcb28e42SPaul Mundt 					      (struct sembuf __user *)ptr,
1791da177e4SLinus Torvalds 					      second, NULL);
1801da177e4SLinus Torvalds 		case SEMTIMEDOP:
181bcb28e42SPaul Mundt 			return sys_semtimedop(first,
182bcb28e42SPaul Mundt 				(struct sembuf __user *)ptr, second,
1831da177e4SLinus Torvalds 			        (const struct timespec __user *)fifth);
1841da177e4SLinus Torvalds 		case SEMGET:
1851da177e4SLinus Torvalds 			return sys_semget (first, second, third);
1861da177e4SLinus Torvalds 		case SEMCTL: {
1871da177e4SLinus Torvalds 			union semun fourth;
1881da177e4SLinus Torvalds 			if (!ptr)
1891da177e4SLinus Torvalds 				return -EINVAL;
190*fa43972fSPaul Mundt 			if (get_user(fourth.__pad, (void __user * __user *) ptr))
1911da177e4SLinus Torvalds 				return -EFAULT;
1921da177e4SLinus Torvalds 			return sys_semctl (first, second, third, fourth);
1931da177e4SLinus Torvalds 			}
1941da177e4SLinus Torvalds 		default:
1951da177e4SLinus Torvalds 			return -EINVAL;
1961da177e4SLinus Torvalds 		}
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	if (call <= MSGCTL)
1991da177e4SLinus Torvalds 		switch (call) {
2001da177e4SLinus Torvalds 		case MSGSND:
2011da177e4SLinus Torvalds 			return sys_msgsnd (first, (struct msgbuf __user *) ptr,
2021da177e4SLinus Torvalds 					  second, third);
2031da177e4SLinus Torvalds 		case MSGRCV:
2041da177e4SLinus Torvalds 			switch (version) {
205bcb28e42SPaul Mundt 			case 0:
206bcb28e42SPaul Mundt 			{
2071da177e4SLinus Torvalds 				struct ipc_kludge tmp;
208bcb28e42SPaul Mundt 
2091da177e4SLinus Torvalds 				if (!ptr)
2101da177e4SLinus Torvalds 					return -EINVAL;
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 				if (copy_from_user(&tmp,
2131da177e4SLinus Torvalds 					(struct ipc_kludge __user *) ptr,
2141da177e4SLinus Torvalds 						   sizeof (tmp)))
2151da177e4SLinus Torvalds 					return -EFAULT;
216bcb28e42SPaul Mundt 
2171da177e4SLinus Torvalds 				return sys_msgrcv (first, tmp.msgp, second,
2181da177e4SLinus Torvalds 						   tmp.msgtyp, third);
2191da177e4SLinus Torvalds 			}
2201da177e4SLinus Torvalds 			default:
2211da177e4SLinus Torvalds 				return sys_msgrcv (first,
2221da177e4SLinus Torvalds 						   (struct msgbuf __user *) ptr,
2231da177e4SLinus Torvalds 						   second, fifth, third);
2241da177e4SLinus Torvalds 			}
2251da177e4SLinus Torvalds 		case MSGGET:
2261da177e4SLinus Torvalds 			return sys_msgget ((key_t) first, second);
2271da177e4SLinus Torvalds 		case MSGCTL:
2281da177e4SLinus Torvalds 			return sys_msgctl (first, second,
2291da177e4SLinus Torvalds 					   (struct msqid_ds __user *) ptr);
2301da177e4SLinus Torvalds 		default:
2311da177e4SLinus Torvalds 			return -EINVAL;
2321da177e4SLinus Torvalds 		}
2331da177e4SLinus Torvalds 	if (call <= SHMCTL)
2341da177e4SLinus Torvalds 		switch (call) {
2351da177e4SLinus Torvalds 		case SHMAT:
2361da177e4SLinus Torvalds 			switch (version) {
2371da177e4SLinus Torvalds 			default: {
2381da177e4SLinus Torvalds 				ulong raddr;
2391da177e4SLinus Torvalds 				ret = do_shmat (first, (char __user *) ptr,
2401da177e4SLinus Torvalds 						 second, &raddr);
2411da177e4SLinus Torvalds 				if (ret)
2421da177e4SLinus Torvalds 					return ret;
2431da177e4SLinus Torvalds 				return put_user (raddr, (ulong __user *) third);
2441da177e4SLinus Torvalds 			}
2451da177e4SLinus Torvalds 			case 1:	/* iBCS2 emulator entry point */
2461da177e4SLinus Torvalds 				if (!segment_eq(get_fs(), get_ds()))
2471da177e4SLinus Torvalds 					return -EINVAL;
2481da177e4SLinus Torvalds 				return do_shmat (first, (char __user *) ptr,
2491da177e4SLinus Torvalds 						  second, (ulong *) third);
2501da177e4SLinus Torvalds 			}
2511da177e4SLinus Torvalds 		case SHMDT:
2521da177e4SLinus Torvalds 			return sys_shmdt ((char __user *)ptr);
2531da177e4SLinus Torvalds 		case SHMGET:
2541da177e4SLinus Torvalds 			return sys_shmget (first, second, third);
2551da177e4SLinus Torvalds 		case SHMCTL:
2561da177e4SLinus Torvalds 			return sys_shmctl (first, second,
2571da177e4SLinus Torvalds 					   (struct shmid_ds __user *) ptr);
2581da177e4SLinus Torvalds 		default:
2591da177e4SLinus Torvalds 			return -EINVAL;
2601da177e4SLinus Torvalds 		}
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	return -EINVAL;
2631da177e4SLinus Torvalds }
2641da177e4SLinus Torvalds 
265*fa43972fSPaul Mundt asmlinkage int sys_uname(struct old_utsname __user *name)
2661da177e4SLinus Torvalds {
2671da177e4SLinus Torvalds 	int err;
2681da177e4SLinus Torvalds 	if (!name)
2691da177e4SLinus Torvalds 		return -EFAULT;
2701da177e4SLinus Torvalds 	down_read(&uts_sem);
271e9ff3990SSerge E. Hallyn 	err = copy_to_user(name, utsname(), sizeof(*name));
2721da177e4SLinus Torvalds 	up_read(&uts_sem);
2731da177e4SLinus Torvalds 	return err?-EFAULT:0;
2741da177e4SLinus Torvalds }
275