1/*
2 *  Implementation of various system calls for Linux/PowerPC
3 *
4 *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
5 *
6 * Derived from "arch/i386/kernel/sys_i386.c"
7 * Adapted from the i386 version by Gary Thomas
8 * Modified by Cort Dougan (cort@cs.nmt.edu)
9 * and Paul Mackerras (paulus@cs.anu.edu.au).
10 *
11 * This file contains various random system calls that
12 * have a non-standard calling sequence on the Linux/PPC
13 * platform.
14 *
15 *  This program is free software; you can redistribute it and/or
16 *  modify it under the terms of the GNU General Public License
17 *  as published by the Free Software Foundation; either version
18 *  2 of the License, or (at your option) any later version.
19 *
20 */
21
22#include <linux/errno.h>
23#include <linux/sched.h>
24#include <linux/syscalls.h>
25#include <linux/mm.h>
26#include <linux/smp.h>
27#include <linux/sem.h>
28#include <linux/msg.h>
29#include <linux/shm.h>
30#include <linux/stat.h>
31#include <linux/mman.h>
32#include <linux/sys.h>
33#include <linux/ipc.h>
34#include <linux/utsname.h>
35#include <linux/file.h>
36#include <linux/init.h>
37#include <linux/personality.h>
38
39#include <asm/uaccess.h>
40#include <asm/ipc.h>
41#include <asm/semaphore.h>
42#include <asm/syscalls.h>
43#include <asm/time.h>
44#include <asm/unistd.h>
45
46/*
47 * sys_ipc() is the de-multiplexer for the SysV IPC calls..
48 *
49 * This is really horribly ugly.
50 */
51int sys_ipc(uint call, int first, unsigned long second, long third,
52	    void __user *ptr, long fifth)
53{
54	int version, ret;
55
56	version = call >> 16; /* hack for backward compatibility */
57	call &= 0xffff;
58
59	ret = -ENOSYS;
60	switch (call) {
61	case SEMOP:
62		ret = sys_semtimedop(first, (struct sembuf __user *)ptr,
63				      (unsigned)second, NULL);
64		break;
65	case SEMTIMEDOP:
66		ret = sys_semtimedop(first, (struct sembuf __user *)ptr,
67				      (unsigned)second,
68				      (const struct timespec __user *) fifth);
69		break;
70	case SEMGET:
71		ret = sys_semget (first, (int)second, third);
72		break;
73	case SEMCTL: {
74		union semun fourth;
75
76		ret = -EINVAL;
77		if (!ptr)
78			break;
79		if ((ret = get_user(fourth.__pad, (void __user * __user *)ptr)))
80			break;
81		ret = sys_semctl(first, (int)second, third, fourth);
82		break;
83	}
84	case MSGSND:
85		ret = sys_msgsnd(first, (struct msgbuf __user *)ptr,
86				 (size_t)second, third);
87		break;
88	case MSGRCV:
89		switch (version) {
90		case 0: {
91			struct ipc_kludge tmp;
92
93			ret = -EINVAL;
94			if (!ptr)
95				break;
96			if ((ret = copy_from_user(&tmp,
97						(struct ipc_kludge __user *) ptr,
98						sizeof (tmp)) ? -EFAULT : 0))
99				break;
100			ret = sys_msgrcv(first, tmp.msgp, (size_t) second,
101					  tmp.msgtyp, third);
102			break;
103		}
104		default:
105			ret = sys_msgrcv (first, (struct msgbuf __user *) ptr,
106					  (size_t)second, fifth, third);
107			break;
108		}
109		break;
110	case MSGGET:
111		ret = sys_msgget((key_t)first, (int)second);
112		break;
113	case MSGCTL:
114		ret = sys_msgctl(first, (int)second,
115				  (struct msqid_ds __user *)ptr);
116		break;
117	case SHMAT: {
118		ulong raddr;
119		ret = do_shmat(first, (char __user *)ptr, (int)second, &raddr);
120		if (ret)
121			break;
122		ret = put_user(raddr, (ulong __user *) third);
123		break;
124	}
125	case SHMDT:
126		ret = sys_shmdt((char __user *)ptr);
127		break;
128	case SHMGET:
129		ret = sys_shmget(first, (size_t)second, third);
130		break;
131	case SHMCTL:
132		ret = sys_shmctl(first, (int)second,
133				 (struct shmid_ds __user *)ptr);
134		break;
135	}
136
137	return ret;
138}
139
140/*
141 * sys_pipe() is the normal C calling standard for creating
142 * a pipe. It's not the way unix traditionally does this, though.
143 */
144int sys_pipe(int __user *fildes)
145{
146	int fd[2];
147	int error;
148
149	error = do_pipe(fd);
150	if (!error) {
151		if (copy_to_user(fildes, fd, 2*sizeof(int)))
152			error = -EFAULT;
153	}
154	return error;
155}
156
157static inline unsigned long do_mmap2(unsigned long addr, size_t len,
158			unsigned long prot, unsigned long flags,
159			unsigned long fd, unsigned long off, int shift)
160{
161	struct file * file = NULL;
162	unsigned long ret = -EINVAL;
163
164	if (shift) {
165		if (off & ((1 << shift) - 1))
166			goto out;
167		off >>= shift;
168	}
169
170	ret = -EBADF;
171	if (!(flags & MAP_ANONYMOUS)) {
172		if (!(file = fget(fd)))
173			goto out;
174	}
175
176	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
177
178	down_write(&current->mm->mmap_sem);
179	ret = do_mmap_pgoff(file, addr, len, prot, flags, off);
180	up_write(&current->mm->mmap_sem);
181	if (file)
182		fput(file);
183out:
184	return ret;
185}
186
187unsigned long sys_mmap2(unsigned long addr, size_t len,
188			unsigned long prot, unsigned long flags,
189			unsigned long fd, unsigned long pgoff)
190{
191	return do_mmap2(addr, len, prot, flags, fd, pgoff, PAGE_SHIFT-12);
192}
193
194unsigned long sys_mmap(unsigned long addr, size_t len,
195		       unsigned long prot, unsigned long flags,
196		       unsigned long fd, off_t offset)
197{
198	return do_mmap2(addr, len, prot, flags, fd, offset, PAGE_SHIFT);
199}
200
201#ifdef CONFIG_PPC32
202/*
203 * Due to some executables calling the wrong select we sometimes
204 * get wrong args.  This determines how the args are being passed
205 * (a single ptr to them all args passed) then calls
206 * sys_select() with the appropriate args. -- Cort
207 */
208int
209ppc_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp, struct timeval __user *tvp)
210{
211	if ( (unsigned long)n >= 4096 )
212	{
213		unsigned long __user *buffer = (unsigned long __user *)n;
214		if (!access_ok(VERIFY_READ, buffer, 5*sizeof(unsigned long))
215		    || __get_user(n, buffer)
216		    || __get_user(inp, ((fd_set __user * __user *)(buffer+1)))
217		    || __get_user(outp, ((fd_set  __user * __user *)(buffer+2)))
218		    || __get_user(exp, ((fd_set  __user * __user *)(buffer+3)))
219		    || __get_user(tvp, ((struct timeval  __user * __user *)(buffer+4))))
220			return -EFAULT;
221	}
222	return sys_select(n, inp, outp, exp, tvp);
223}
224#endif
225
226#ifdef CONFIG_PPC64
227long ppc64_personality(unsigned long personality)
228{
229	long ret;
230
231	if (personality(current->personality) == PER_LINUX32
232	    && personality == PER_LINUX)
233		personality = PER_LINUX32;
234	ret = sys_personality(personality);
235	if (ret == PER_LINUX32)
236		ret = PER_LINUX;
237	return ret;
238}
239#endif
240
241#ifdef CONFIG_PPC64
242#define OVERRIDE_MACHINE    (personality(current->personality) == PER_LINUX32)
243#else
244#define OVERRIDE_MACHINE    0
245#endif
246
247static inline int override_machine(char __user *mach)
248{
249	if (OVERRIDE_MACHINE) {
250		/* change ppc64 to ppc */
251		if (__put_user(0, mach+3) || __put_user(0, mach+4))
252			return -EFAULT;
253	}
254	return 0;
255}
256
257long ppc_newuname(struct new_utsname __user * name)
258{
259	int err = 0;
260
261	down_read(&uts_sem);
262	if (copy_to_user(name, utsname(), sizeof(*name)))
263		err = -EFAULT;
264	up_read(&uts_sem);
265	if (!err)
266		err = override_machine(name->machine);
267	return err;
268}
269
270int sys_uname(struct old_utsname __user *name)
271{
272	int err = 0;
273
274	down_read(&uts_sem);
275	if (copy_to_user(name, utsname(), sizeof(*name)))
276		err = -EFAULT;
277	up_read(&uts_sem);
278	if (!err)
279		err = override_machine(name->machine);
280	return err;
281}
282
283int sys_olduname(struct oldold_utsname __user *name)
284{
285	int error;
286
287	if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
288		return -EFAULT;
289
290	down_read(&uts_sem);
291	error = __copy_to_user(&name->sysname, &utsname()->sysname,
292			       __OLD_UTS_LEN);
293	error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
294	error |= __copy_to_user(&name->nodename, &utsname()->nodename,
295				__OLD_UTS_LEN);
296	error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
297	error |= __copy_to_user(&name->release, &utsname()->release,
298				__OLD_UTS_LEN);
299	error |= __put_user(0, name->release + __OLD_UTS_LEN);
300	error |= __copy_to_user(&name->version, &utsname()->version,
301				__OLD_UTS_LEN);
302	error |= __put_user(0, name->version + __OLD_UTS_LEN);
303	error |= __copy_to_user(&name->machine, &utsname()->machine,
304				__OLD_UTS_LEN);
305	error |= override_machine(name->machine);
306	up_read(&uts_sem);
307
308	return error? -EFAULT: 0;
309}
310
311long ppc_fadvise64_64(int fd, int advice, u32 offset_high, u32 offset_low,
312		      u32 len_high, u32 len_low)
313{
314	return sys_fadvise64(fd, (u64)offset_high << 32 | offset_low,
315			     (u64)len_high << 32 | len_low, advice);
316}
317
318void do_show_syscall(unsigned long r3, unsigned long r4, unsigned long r5,
319		     unsigned long r6, unsigned long r7, unsigned long r8,
320		     struct pt_regs *regs)
321{
322	printk("syscall %ld(%lx, %lx, %lx, %lx, %lx, %lx) regs=%p current=%p"
323	       " cpu=%d\n", regs->gpr[0], r3, r4, r5, r6, r7, r8, regs,
324	       current, smp_processor_id());
325}
326
327void do_show_syscall_exit(unsigned long r3)
328{
329	printk(" -> %lx, current=%p cpu=%d\n", r3, current, smp_processor_id());
330}
331