xref: /openbmc/linux/arch/sh/kernel/sys_sh.c (revision bcb28e42be8c1cce6cc523c1b656980011464016)
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>
261da177e4SLinus Torvalds #include <asm/uaccess.h>
27fe74290dSArnd Bergmann #include <asm/unistd.h>
281da177e4SLinus Torvalds 
29f3c25758SPaul Mundt unsigned long shm_align_mask = PAGE_SIZE - 1;	/* Sane caches */
30f3c25758SPaul Mundt EXPORT_SYMBOL(shm_align_mask);
31f3c25758SPaul Mundt 
32710ee0ccSPaul Mundt #ifdef CONFIG_MMU
331da177e4SLinus Torvalds /*
34f3c25758SPaul Mundt  * To avoid cache aliases, we map the shared page with same color.
351da177e4SLinus Torvalds  */
36f3c25758SPaul Mundt #define COLOUR_ALIGN(addr, pgoff)				\
37f3c25758SPaul Mundt 	((((addr) + shm_align_mask) & ~shm_align_mask) +	\
38f3c25758SPaul Mundt 	 (((pgoff) << PAGE_SHIFT) & shm_align_mask))
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
411da177e4SLinus Torvalds 	unsigned long len, unsigned long pgoff, unsigned long flags)
421da177e4SLinus Torvalds {
431da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
441da177e4SLinus Torvalds 	struct vm_area_struct *vma;
451da177e4SLinus Torvalds 	unsigned long start_addr;
46f3c25758SPaul Mundt 	int do_colour_align;
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds 	if (flags & MAP_FIXED) {
491da177e4SLinus Torvalds 		/* We do not accept a shared mapping if it would violate
501da177e4SLinus Torvalds 		 * cache aliasing constraints.
511da177e4SLinus Torvalds 		 */
52f3c25758SPaul Mundt 		if ((flags & MAP_SHARED) && (addr & shm_align_mask))
531da177e4SLinus Torvalds 			return -EINVAL;
541da177e4SLinus Torvalds 		return addr;
551da177e4SLinus Torvalds 	}
561da177e4SLinus Torvalds 
57f3c25758SPaul Mundt 	if (unlikely(len > TASK_SIZE))
581da177e4SLinus Torvalds 		return -ENOMEM;
591da177e4SLinus Torvalds 
60f3c25758SPaul Mundt 	do_colour_align = 0;
61f3c25758SPaul Mundt 	if (filp || (flags & MAP_SHARED))
62f3c25758SPaul Mundt 		do_colour_align = 1;
63f3c25758SPaul Mundt 
641da177e4SLinus Torvalds 	if (addr) {
65f3c25758SPaul Mundt 		if (do_colour_align)
66f3c25758SPaul Mundt 			addr = COLOUR_ALIGN(addr, pgoff);
671da177e4SLinus Torvalds 		else
68f3c25758SPaul Mundt 			addr = PAGE_ALIGN(addr);
69f3c25758SPaul Mundt 
701da177e4SLinus Torvalds 		vma = find_vma(mm, addr);
711da177e4SLinus Torvalds 		if (TASK_SIZE - len >= addr &&
721da177e4SLinus Torvalds 		    (!vma || addr + len <= vma->vm_start))
731da177e4SLinus Torvalds 			return addr;
741da177e4SLinus Torvalds 	}
75f3c25758SPaul Mundt 
76f3c25758SPaul Mundt 	if (len > mm->cached_hole_size) {
77f3c25758SPaul Mundt 		start_addr = addr = mm->free_area_cache;
78f3c25758SPaul Mundt 	} else {
791363c3cdSWolfgang Wander 	        mm->cached_hole_size = 0;
80f3c25758SPaul Mundt 		start_addr = addr = TASK_UNMAPPED_BASE;
811363c3cdSWolfgang Wander 	}
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds full_search:
84f3c25758SPaul Mundt 	if (do_colour_align)
85f3c25758SPaul Mundt 		addr = COLOUR_ALIGN(addr, pgoff);
86f3c25758SPaul Mundt 	else
87f3c25758SPaul Mundt 		addr = PAGE_ALIGN(mm->free_area_cache);
88f3c25758SPaul Mundt 
891da177e4SLinus Torvalds 	for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
901da177e4SLinus Torvalds 		/* At this point:  (!vma || addr < vma->vm_end). */
91f3c25758SPaul Mundt 		if (unlikely(TASK_SIZE - len < addr)) {
921da177e4SLinus Torvalds 			/*
931da177e4SLinus Torvalds 			 * Start a new search - just in case we missed
941da177e4SLinus Torvalds 			 * some holes.
951da177e4SLinus Torvalds 			 */
961da177e4SLinus Torvalds 			if (start_addr != TASK_UNMAPPED_BASE) {
971da177e4SLinus Torvalds 				start_addr = addr = TASK_UNMAPPED_BASE;
981363c3cdSWolfgang Wander 				mm->cached_hole_size = 0;
991da177e4SLinus Torvalds 				goto full_search;
1001da177e4SLinus Torvalds 			}
1011da177e4SLinus Torvalds 			return -ENOMEM;
1021da177e4SLinus Torvalds 		}
103f3c25758SPaul Mundt 		if (likely(!vma || addr + len <= vma->vm_start)) {
1041da177e4SLinus Torvalds 			/*
1051da177e4SLinus Torvalds 			 * Remember the place where we stopped the search:
1061da177e4SLinus Torvalds 			 */
1071da177e4SLinus Torvalds 			mm->free_area_cache = addr + len;
1081da177e4SLinus Torvalds 			return addr;
1091da177e4SLinus Torvalds 		}
1101363c3cdSWolfgang Wander 		if (addr + mm->cached_hole_size < vma->vm_start)
1111363c3cdSWolfgang Wander 		        mm->cached_hole_size = vma->vm_start - addr;
1121363c3cdSWolfgang Wander 
1131da177e4SLinus Torvalds 		addr = vma->vm_end;
114f3c25758SPaul Mundt 		if (do_colour_align)
115f3c25758SPaul Mundt 			addr = COLOUR_ALIGN(addr, pgoff);
1161da177e4SLinus Torvalds 	}
1171da177e4SLinus Torvalds }
118710ee0ccSPaul Mundt #endif /* CONFIG_MMU */
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds static inline long
1211da177e4SLinus Torvalds do_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
1221da177e4SLinus Torvalds 	 unsigned long flags, int fd, unsigned long pgoff)
1231da177e4SLinus Torvalds {
1241da177e4SLinus Torvalds 	int error = -EBADF;
1251da177e4SLinus Torvalds 	struct file *file = NULL;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1281da177e4SLinus Torvalds 	if (!(flags & MAP_ANONYMOUS)) {
1291da177e4SLinus Torvalds 		file = fget(fd);
1301da177e4SLinus Torvalds 		if (!file)
1311da177e4SLinus Torvalds 			goto out;
1321da177e4SLinus Torvalds 	}
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	down_write(&current->mm->mmap_sem);
1351da177e4SLinus Torvalds 	error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1361da177e4SLinus Torvalds 	up_write(&current->mm->mmap_sem);
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	if (file)
1391da177e4SLinus Torvalds 		fput(file);
1401da177e4SLinus Torvalds out:
1411da177e4SLinus Torvalds 	return error;
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds asmlinkage int old_mmap(unsigned long addr, unsigned long len,
1451da177e4SLinus Torvalds 	unsigned long prot, unsigned long flags,
1461da177e4SLinus Torvalds 	int fd, unsigned long off)
1471da177e4SLinus Torvalds {
1481da177e4SLinus Torvalds 	if (off & ~PAGE_MASK)
1491da177e4SLinus Torvalds 		return -EINVAL;
1501da177e4SLinus Torvalds 	return do_mmap2(addr, len, prot, flags, fd, off>>PAGE_SHIFT);
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds asmlinkage long sys_mmap2(unsigned long addr, unsigned long len,
1541da177e4SLinus Torvalds 	unsigned long prot, unsigned long flags,
1551da177e4SLinus Torvalds 	unsigned long fd, unsigned long pgoff)
1561da177e4SLinus Torvalds {
1571da177e4SLinus Torvalds 	return do_mmap2(addr, len, prot, flags, fd, pgoff);
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds /*
1611da177e4SLinus Torvalds  * sys_ipc() is the de-multiplexer for the SysV IPC calls..
1621da177e4SLinus Torvalds  *
1631da177e4SLinus Torvalds  * This is really horribly ugly.
1641da177e4SLinus Torvalds  */
1651da177e4SLinus Torvalds asmlinkage int sys_ipc(uint call, int first, int second,
1661da177e4SLinus Torvalds 		       int third, void __user *ptr, long fifth)
1671da177e4SLinus Torvalds {
1681da177e4SLinus Torvalds 	int version, ret;
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	version = call >> 16; /* hack for backward compatibility */
1711da177e4SLinus Torvalds 	call &= 0xffff;
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	if (call <= SEMCTL)
1741da177e4SLinus Torvalds 		switch (call) {
1751da177e4SLinus Torvalds 		case SEMOP:
176*bcb28e42SPaul Mundt 			return sys_semtimedop(first,
177*bcb28e42SPaul Mundt 					      (struct sembuf __user *)ptr,
1781da177e4SLinus Torvalds 					      second, NULL);
1791da177e4SLinus Torvalds 		case SEMTIMEDOP:
180*bcb28e42SPaul Mundt 			return sys_semtimedop(first,
181*bcb28e42SPaul Mundt 				(struct sembuf __user *)ptr, second,
1821da177e4SLinus Torvalds 			        (const struct timespec __user *)fifth);
1831da177e4SLinus Torvalds 		case SEMGET:
1841da177e4SLinus Torvalds 			return sys_semget (first, second, third);
1851da177e4SLinus Torvalds 		case SEMCTL: {
1861da177e4SLinus Torvalds 			union semun fourth;
1871da177e4SLinus Torvalds 			if (!ptr)
1881da177e4SLinus Torvalds 				return -EINVAL;
1891da177e4SLinus Torvalds 			if (get_user(fourth.__pad, (void * __user *) ptr))
1901da177e4SLinus Torvalds 				return -EFAULT;
1911da177e4SLinus Torvalds 			return sys_semctl (first, second, third, fourth);
1921da177e4SLinus Torvalds 			}
1931da177e4SLinus Torvalds 		default:
1941da177e4SLinus Torvalds 			return -EINVAL;
1951da177e4SLinus Torvalds 		}
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	if (call <= MSGCTL)
1981da177e4SLinus Torvalds 		switch (call) {
1991da177e4SLinus Torvalds 		case MSGSND:
2001da177e4SLinus Torvalds 			return sys_msgsnd (first, (struct msgbuf __user *) ptr,
2011da177e4SLinus Torvalds 					  second, third);
2021da177e4SLinus Torvalds 		case MSGRCV:
2031da177e4SLinus Torvalds 			switch (version) {
204*bcb28e42SPaul Mundt 			case 0:
205*bcb28e42SPaul Mundt 			{
2061da177e4SLinus Torvalds 				struct ipc_kludge tmp;
207*bcb28e42SPaul Mundt 
2081da177e4SLinus Torvalds 				if (!ptr)
2091da177e4SLinus Torvalds 					return -EINVAL;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 				if (copy_from_user(&tmp,
2121da177e4SLinus Torvalds 					(struct ipc_kludge __user *) ptr,
2131da177e4SLinus Torvalds 						   sizeof (tmp)))
2141da177e4SLinus Torvalds 					return -EFAULT;
215*bcb28e42SPaul Mundt 
2161da177e4SLinus Torvalds 				return sys_msgrcv (first, tmp.msgp, second,
2171da177e4SLinus Torvalds 						   tmp.msgtyp, third);
2181da177e4SLinus Torvalds 			}
2191da177e4SLinus Torvalds 			default:
2201da177e4SLinus Torvalds 				return sys_msgrcv (first,
2211da177e4SLinus Torvalds 						   (struct msgbuf __user *) ptr,
2221da177e4SLinus Torvalds 						   second, fifth, third);
2231da177e4SLinus Torvalds 			}
2241da177e4SLinus Torvalds 		case MSGGET:
2251da177e4SLinus Torvalds 			return sys_msgget ((key_t) first, second);
2261da177e4SLinus Torvalds 		case MSGCTL:
2271da177e4SLinus Torvalds 			return sys_msgctl (first, second,
2281da177e4SLinus Torvalds 					   (struct msqid_ds __user *) ptr);
2291da177e4SLinus Torvalds 		default:
2301da177e4SLinus Torvalds 			return -EINVAL;
2311da177e4SLinus Torvalds 		}
2321da177e4SLinus Torvalds 	if (call <= SHMCTL)
2331da177e4SLinus Torvalds 		switch (call) {
2341da177e4SLinus Torvalds 		case SHMAT:
2351da177e4SLinus Torvalds 			switch (version) {
2361da177e4SLinus Torvalds 			default: {
2371da177e4SLinus Torvalds 				ulong raddr;
2381da177e4SLinus Torvalds 				ret = do_shmat (first, (char __user *) ptr,
2391da177e4SLinus Torvalds 						 second, &raddr);
2401da177e4SLinus Torvalds 				if (ret)
2411da177e4SLinus Torvalds 					return ret;
2421da177e4SLinus Torvalds 				return put_user (raddr, (ulong __user *) third);
2431da177e4SLinus Torvalds 			}
2441da177e4SLinus Torvalds 			case 1:	/* iBCS2 emulator entry point */
2451da177e4SLinus Torvalds 				if (!segment_eq(get_fs(), get_ds()))
2461da177e4SLinus Torvalds 					return -EINVAL;
2471da177e4SLinus Torvalds 				return do_shmat (first, (char __user *) ptr,
2481da177e4SLinus Torvalds 						  second, (ulong *) third);
2491da177e4SLinus Torvalds 			}
2501da177e4SLinus Torvalds 		case SHMDT:
2511da177e4SLinus Torvalds 			return sys_shmdt ((char __user *)ptr);
2521da177e4SLinus Torvalds 		case SHMGET:
2531da177e4SLinus Torvalds 			return sys_shmget (first, second, third);
2541da177e4SLinus Torvalds 		case SHMCTL:
2551da177e4SLinus Torvalds 			return sys_shmctl (first, second,
2561da177e4SLinus Torvalds 					   (struct shmid_ds __user *) ptr);
2571da177e4SLinus Torvalds 		default:
2581da177e4SLinus Torvalds 			return -EINVAL;
2591da177e4SLinus Torvalds 		}
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	return -EINVAL;
2621da177e4SLinus Torvalds }
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds asmlinkage int sys_uname(struct old_utsname * name)
2651da177e4SLinus Torvalds {
2661da177e4SLinus Torvalds 	int err;
2671da177e4SLinus Torvalds 	if (!name)
2681da177e4SLinus Torvalds 		return -EFAULT;
2691da177e4SLinus Torvalds 	down_read(&uts_sem);
270e9ff3990SSerge E. Hallyn 	err = copy_to_user(name, utsname(), sizeof (*name));
2711da177e4SLinus Torvalds 	up_read(&uts_sem);
2721da177e4SLinus Torvalds 	return err?-EFAULT:0;
2731da177e4SLinus Torvalds }
274