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 * arch/sh64/kernel/sys_sh64.c
7 *
8 * Copyright (C) 2000, 2001  Paolo Alberelli
9 *
10 * This file contains various random system calls that
11 * have a non-standard calling sequence on the Linux/SH5
12 * platform.
13 *
14 * Mostly taken from i386 version.
15 *
16 */
17
18#include <linux/errno.h>
19#include <linux/rwsem.h>
20#include <linux/sched.h>
21#include <linux/mm.h>
22#include <linux/smp.h>
23#include <linux/sem.h>
24#include <linux/msg.h>
25#include <linux/shm.h>
26#include <linux/stat.h>
27#include <linux/mman.h>
28#include <linux/file.h>
29#include <linux/utsname.h>
30#include <linux/syscalls.h>
31#include <asm/uaccess.h>
32#include <asm/ipc.h>
33#include <asm/ptrace.h>
34#include <asm/unistd.h>
35
36#define REG_3	3
37
38/*
39 * sys_pipe() is the normal C calling standard for creating
40 * a pipe. It's not the way Unix traditionally does this, though.
41 */
42#ifdef NEW_PIPE_IMPLEMENTATION
43asmlinkage int sys_pipe(unsigned long * fildes,
44			unsigned long   dummy_r3,
45			unsigned long   dummy_r4,
46			unsigned long   dummy_r5,
47			unsigned long   dummy_r6,
48			unsigned long   dummy_r7,
49			struct pt_regs * regs)	   /* r8 = pt_regs  forced by entry.S */
50{
51	int fd[2];
52	int ret;
53
54	ret = do_pipe(fd);
55	if (ret == 0)
56		/*
57		 ***********************************************************************
58		 *   To avoid the copy_to_user we prefer to break the ABIs convention, *
59		 *   packing the valid pair of file IDs into a single register (r3);   *
60		 *   while r2 is the return code as defined by the sh5-ABIs.	       *
61		 *   BE CAREFUL: pipe stub, into glibc, must be aware of this solution *
62		 ***********************************************************************
63
64#ifdef __LITTLE_ENDIAN__
65		regs->regs[REG_3] = (((unsigned long long) fd[1]) << 32) | ((unsigned long long) fd[0]);
66#else
67		regs->regs[REG_3] = (((unsigned long long) fd[0]) << 32) | ((unsigned long long) fd[1]);
68#endif
69
70		*/
71	       /* although not very clever this is endianess independent */
72		regs->regs[REG_3] = (unsigned long long) *((unsigned long long *) fd);
73
74	return ret;
75}
76
77#else
78asmlinkage int sys_pipe(unsigned long * fildes)
79{
80        int fd[2];
81        int error;
82
83        error = do_pipe(fd);
84        if (!error) {
85                if (copy_to_user(fildes, fd, 2*sizeof(int)))
86                        error = -EFAULT;
87        }
88        return error;
89}
90
91#endif
92
93/*
94 * To avoid cache alias, we map the shard page with same color.
95 */
96#define COLOUR_ALIGN(addr)	(((addr)+SHMLBA-1)&~(SHMLBA-1))
97
98unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
99	unsigned long len, unsigned long pgoff, unsigned long flags)
100{
101	struct vm_area_struct *vma;
102
103	if (flags & MAP_FIXED) {
104		/* We do not accept a shared mapping if it would violate
105		 * cache aliasing constraints.
106		 */
107		if ((flags & MAP_SHARED) && (addr & (SHMLBA - 1)))
108			return -EINVAL;
109		return addr;
110	}
111
112	if (len > TASK_SIZE)
113		return -ENOMEM;
114	if (!addr)
115		addr = TASK_UNMAPPED_BASE;
116
117	if (flags & MAP_PRIVATE)
118		addr = PAGE_ALIGN(addr);
119	else
120		addr = COLOUR_ALIGN(addr);
121
122	for (vma = find_vma(current->mm, addr); ; vma = vma->vm_next) {
123		/* At this point:  (!vma || addr < vma->vm_end). */
124		if (TASK_SIZE - len < addr)
125			return -ENOMEM;
126		if (!vma || addr + len <= vma->vm_start)
127			return addr;
128		addr = vma->vm_end;
129		if (!(flags & MAP_PRIVATE))
130			addr = COLOUR_ALIGN(addr);
131	}
132}
133
134/* common code for old and new mmaps */
135static inline long do_mmap2(
136	unsigned long addr, unsigned long len,
137	unsigned long prot, unsigned long flags,
138	unsigned long fd, unsigned long pgoff)
139{
140	int error = -EBADF;
141	struct file * file = NULL;
142
143	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
144	if (!(flags & MAP_ANONYMOUS)) {
145		file = fget(fd);
146		if (!file)
147			goto out;
148	}
149
150	down_write(&current->mm->mmap_sem);
151	error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
152	up_write(&current->mm->mmap_sem);
153
154	if (file)
155		fput(file);
156out:
157	return error;
158}
159
160asmlinkage long sys_mmap2(unsigned long addr, unsigned long len,
161	unsigned long prot, unsigned long flags,
162	unsigned long fd, unsigned long pgoff)
163{
164	return do_mmap2(addr, len, prot, flags, fd, pgoff);
165}
166
167asmlinkage int old_mmap(unsigned long addr, unsigned long len,
168	unsigned long prot, unsigned long flags,
169	int fd, unsigned long off)
170{
171	if (off & ~PAGE_MASK)
172		return -EINVAL;
173	return do_mmap2(addr, len, prot, flags, fd, off>>PAGE_SHIFT);
174}
175
176/*
177 * sys_ipc() is the de-multiplexer for the SysV IPC calls..
178 *
179 * This is really horribly ugly.
180 */
181asmlinkage int sys_ipc(uint call, int first, int second,
182		       int third, void __user *ptr, long fifth)
183{
184	int version, ret;
185
186	version = call >> 16; /* hack for backward compatibility */
187	call &= 0xffff;
188
189	if (call <= SEMCTL)
190		switch (call) {
191		case SEMOP:
192			return sys_semtimedop(first, (struct sembuf __user *)ptr,
193					      second, NULL);
194		case SEMTIMEDOP:
195			return sys_semtimedop(first, (struct sembuf __user *)ptr,
196					      second,
197					      (const struct timespec __user *)fifth);
198		case SEMGET:
199			return sys_semget (first, second, third);
200		case SEMCTL: {
201			union semun fourth;
202			if (!ptr)
203				return -EINVAL;
204			if (get_user(fourth.__pad, (void * __user *) ptr))
205				return -EFAULT;
206			return sys_semctl (first, second, third, fourth);
207			}
208		default:
209			return -EINVAL;
210		}
211
212	if (call <= MSGCTL)
213		switch (call) {
214		case MSGSND:
215			return sys_msgsnd (first, (struct msgbuf __user *) ptr,
216					  second, third);
217		case MSGRCV:
218			switch (version) {
219			case 0: {
220				struct ipc_kludge tmp;
221				if (!ptr)
222					return -EINVAL;
223
224				if (copy_from_user(&tmp,
225						   (struct ipc_kludge __user *) ptr,
226						   sizeof (tmp)))
227					return -EFAULT;
228				return sys_msgrcv (first, tmp.msgp, second,
229						   tmp.msgtyp, third);
230				}
231			default:
232				return sys_msgrcv (first,
233						   (struct msgbuf __user *) ptr,
234						   second, fifth, third);
235			}
236		case MSGGET:
237			return sys_msgget ((key_t) first, second);
238		case MSGCTL:
239			return sys_msgctl (first, second,
240					   (struct msqid_ds __user *) ptr);
241		default:
242			return -EINVAL;
243		}
244	if (call <= SHMCTL)
245		switch (call) {
246		case SHMAT:
247			switch (version) {
248			default: {
249				ulong raddr;
250				ret = do_shmat (first, (char __user *) ptr,
251						 second, &raddr);
252				if (ret)
253					return ret;
254				return put_user (raddr, (ulong __user *) third);
255			}
256			case 1:	/* iBCS2 emulator entry point */
257				if (!segment_eq(get_fs(), get_ds()))
258					return -EINVAL;
259				return do_shmat (first, (char __user *) ptr,
260						  second, (ulong *) third);
261			}
262		case SHMDT:
263			return sys_shmdt ((char __user *)ptr);
264		case SHMGET:
265			return sys_shmget (first, second, third);
266		case SHMCTL:
267			return sys_shmctl (first, second,
268					   (struct shmid_ds __user *) ptr);
269		default:
270			return -EINVAL;
271		}
272
273	return -EINVAL;
274}
275
276asmlinkage int sys_uname(struct old_utsname * name)
277{
278	int err;
279	if (!name)
280		return -EFAULT;
281	down_read(&uts_sem);
282	err = copy_to_user(name, utsname(), sizeof (*name));
283	up_read(&uts_sem);
284	return err?-EFAULT:0;
285}
286
287/*
288 * Do a system call from kernel instead of calling sys_execve so we
289 * end up with proper pt_regs.
290 */
291int kernel_execve(const char *filename, char *const argv[], char *const envp[])
292{
293	register unsigned long __sc0 __asm__ ("r9") = ((0x13 << 16) | __NR_execve);
294	register unsigned long __sc2 __asm__ ("r2") = (unsigned long) filename;
295	register unsigned long __sc3 __asm__ ("r3") = (unsigned long) argv;
296	register unsigned long __sc4 __asm__ ("r4") = (unsigned long) envp;
297	__asm__ __volatile__ ("trapa	%1 !\t\t\t execve(%2,%3,%4)"
298	: "=r" (__sc0)
299	: "r" (__sc0), "r" (__sc2), "r" (__sc3), "r" (__sc4) );
300	__asm__ __volatile__ ("!dummy	%0 %1 %2 %3"
301	: : "r" (__sc0), "r" (__sc2), "r" (__sc3), "r" (__sc4) : "memory");
302	return __sc0;
303}
304