1/* $Id: sys_sparc.c,v 1.1.1.1 2008/10/15 03:26:18 james26_jang Exp $
2 * linux/arch/sparc/kernel/sys_sparc.c
3 *
4 * This file contains various random system calls that
5 * have a non-standard calling sequence on the Linux/sparc
6 * platform.
7 */
8
9#include <linux/errno.h>
10#include <linux/types.h>
11#include <linux/sched.h>
12#include <linux/mm.h>
13#include <linux/fs.h>
14#include <linux/file.h>
15#include <linux/sem.h>
16#include <linux/msg.h>
17#include <linux/shm.h>
18#include <linux/stat.h>
19#include <linux/mman.h>
20#include <linux/utsname.h>
21#include <linux/smp.h>
22#include <linux/smp_lock.h>
23
24#include <asm/uaccess.h>
25#include <asm/ipc.h>
26
27/* #define DEBUG_UNIMP_SYSCALL */
28
29asmlinkage unsigned long sys_getpagesize(void)
30{
31	return PAGE_SIZE; /* Possibly older binaries want 8192 on sun4's? */
32}
33
34#define COLOUR_ALIGN(addr)      (((addr)+SHMLBA-1)&~(SHMLBA-1))
35
36unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags)
37{
38	struct vm_area_struct * vmm;
39
40	if (flags & MAP_FIXED) {
41		/* We do not accept a shared mapping if it would violate
42		 * cache aliasing constraints.
43		 */
44		if ((flags & MAP_SHARED) && (addr & (SHMLBA - 1)))
45			return -EINVAL;
46		return addr;
47	}
48
49	/* See asm-sparc/uaccess.h */
50	if (len > TASK_SIZE - PAGE_SIZE)
51		return -ENOMEM;
52	if (ARCH_SUN4C_SUN4 && len > 0x20000000)
53		return -ENOMEM;
54	if (!addr)
55		addr = TASK_UNMAPPED_BASE;
56
57	if (flags & MAP_SHARED)
58		addr = COLOUR_ALIGN(addr);
59	else
60		addr = PAGE_ALIGN(addr);
61
62	for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
63		/* At this point:  (!vmm || addr < vmm->vm_end). */
64		if (ARCH_SUN4C_SUN4 && addr < 0xe0000000 && 0x20000000 - len < addr) {
65			addr = PAGE_OFFSET;
66			vmm = find_vma(current->mm, PAGE_OFFSET);
67		}
68		if (TASK_SIZE - PAGE_SIZE - len < addr)
69			return -ENOMEM;
70		if (!vmm || addr + len <= vmm->vm_start)
71			return addr;
72		addr = vmm->vm_end;
73		if (flags & MAP_SHARED)
74			addr = COLOUR_ALIGN(addr);
75	}
76}
77
78extern asmlinkage unsigned long sys_brk(unsigned long brk);
79
80asmlinkage unsigned long sparc_brk(unsigned long brk)
81{
82	if(ARCH_SUN4C_SUN4) {
83		if ((brk & 0xe0000000) != (current->mm->brk & 0xe0000000))
84			return current->mm->brk;
85	}
86	return sys_brk(brk);
87}
88
89/*
90 * sys_pipe() is the normal C calling standard for creating
91 * a pipe. It's not the way unix traditionally does this, though.
92 */
93asmlinkage int sparc_pipe(struct pt_regs *regs)
94{
95	int fd[2];
96	int error;
97
98	error = do_pipe(fd);
99	if (error)
100		goto out;
101	regs->u_regs[UREG_I1] = fd[1];
102	error = fd[0];
103out:
104	return error;
105}
106
107/*
108 * sys_ipc() is the de-multiplexer for the SysV IPC calls..
109 *
110 * This is really horribly ugly.
111 */
112
113asmlinkage int sys_ipc (uint call, int first, int second, int third, void *ptr, long fifth)
114{
115	int version, err;
116
117	version = call >> 16; /* hack for backward compatibility */
118	call &= 0xffff;
119
120	if (call <= SEMCTL)
121		switch (call) {
122		case SEMOP:
123			err = sys_semop (first, (struct sembuf *)ptr, second);
124			goto out;
125		case SEMGET:
126			err = sys_semget (first, second, third);
127			goto out;
128		case SEMCTL: {
129			union semun fourth;
130			err = -EINVAL;
131			if (!ptr)
132				goto out;
133			err = -EFAULT;
134			if(get_user(fourth.__pad, (void **)ptr))
135				goto out;
136			err = sys_semctl (first, second, third, fourth);
137			goto out;
138			}
139		default:
140			err = -EINVAL;
141			goto out;
142		}
143	if (call <= MSGCTL)
144		switch (call) {
145		case MSGSND:
146			err = sys_msgsnd (first, (struct msgbuf *) ptr,
147					  second, third);
148			goto out;
149		case MSGRCV:
150			switch (version) {
151			case 0: {
152				struct ipc_kludge tmp;
153				err = -EINVAL;
154				if (!ptr)
155					goto out;
156				err = -EFAULT;
157				if(copy_from_user(&tmp,(struct ipc_kludge *) ptr, sizeof (tmp)))
158					goto out;
159				err = sys_msgrcv (first, tmp.msgp, second, tmp.msgtyp, third);
160				goto out;
161				}
162			case 1: default:
163				err = sys_msgrcv (first, (struct msgbuf *) ptr, second, fifth, third);
164				goto out;
165			}
166		case MSGGET:
167			err = sys_msgget ((key_t) first, second);
168			goto out;
169		case MSGCTL:
170			err = sys_msgctl (first, second, (struct msqid_ds *) ptr);
171			goto out;
172		default:
173			err = -EINVAL;
174			goto out;
175		}
176	if (call <= SHMCTL)
177		switch (call) {
178		case SHMAT:
179			switch (version) {
180			case 0: default: {
181				ulong raddr;
182				err = sys_shmat (first, (char *) ptr, second, &raddr);
183				if (err)
184					goto out;
185				err = -EFAULT;
186				if(put_user (raddr, (ulong *) third))
187					goto out;
188				err = 0;
189				goto out;
190				}
191			case 1:	/* iBCS2 emulator entry point */
192				err = sys_shmat (first, (char *) ptr, second, (ulong *) third);
193				goto out;
194			}
195		case SHMDT:
196			err = sys_shmdt ((char *)ptr);
197			goto out;
198		case SHMGET:
199			err = sys_shmget (first, second, third);
200			goto out;
201		case SHMCTL:
202			err = sys_shmctl (first, second, (struct shmid_ds *) ptr);
203			goto out;
204		default:
205			err = -EINVAL;
206			goto out;
207		}
208	else
209		err = -EINVAL;
210out:
211	return err;
212}
213
214/* Linux version of mmap */
215static unsigned long do_mmap2(unsigned long addr, unsigned long len,
216	unsigned long prot, unsigned long flags, unsigned long fd,
217	unsigned long pgoff)
218{
219	struct file * file = NULL;
220	unsigned long retval = -EBADF;
221
222	if (!(flags & MAP_ANONYMOUS)) {
223		file = fget(fd);
224		if (!file)
225			goto out;
226	}
227
228	retval = -EINVAL;
229	len = PAGE_ALIGN(len);
230	if (ARCH_SUN4C_SUN4 &&
231	    (len > 0x20000000 ||
232	     ((flags & MAP_FIXED) &&
233	      addr < 0xe0000000 && addr + len > 0x20000000)))
234		goto out_putf;
235
236	/* See asm-sparc/uaccess.h */
237	if (len > TASK_SIZE - PAGE_SIZE || addr + len > TASK_SIZE - PAGE_SIZE)
238		goto out_putf;
239
240	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
241
242	down_write(&current->mm->mmap_sem);
243	retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
244	up_write(&current->mm->mmap_sem);
245
246out_putf:
247	if (file)
248		fput(file);
249out:
250	return retval;
251}
252
253asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
254	unsigned long prot, unsigned long flags, unsigned long fd,
255	unsigned long pgoff)
256{
257	/* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
258	   we have. */
259	return do_mmap2(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT - 12));
260}
261
262asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
263	unsigned long prot, unsigned long flags, unsigned long fd,
264	unsigned long off)
265{
266	return do_mmap2(addr, len, prot, flags, fd, off >> PAGE_SHIFT);
267}
268
269extern unsigned long do_mremap(unsigned long addr,
270	unsigned long old_len, unsigned long new_len,
271	unsigned long flags, unsigned long new_addr);
272
273asmlinkage unsigned long sparc_mremap(unsigned long addr,
274	unsigned long old_len, unsigned long new_len,
275	unsigned long flags, unsigned long new_addr)
276{
277	struct vm_area_struct *vma;
278	unsigned long ret = -EINVAL;
279	if (ARCH_SUN4C_SUN4) {
280		if (old_len > 0x20000000 || new_len > 0x20000000)
281			goto out;
282		if (addr < 0xe0000000 && addr + old_len > 0x20000000)
283			goto out;
284	}
285	if (old_len > TASK_SIZE - PAGE_SIZE ||
286	    new_len > TASK_SIZE - PAGE_SIZE)
287		goto out;
288	down_write(&current->mm->mmap_sem);
289	if (flags & MREMAP_FIXED) {
290		if (ARCH_SUN4C_SUN4 &&
291		    new_addr < 0xe0000000 &&
292		    new_addr + new_len > 0x20000000)
293			goto out_sem;
294		if (new_addr + new_len > TASK_SIZE - PAGE_SIZE)
295			goto out_sem;
296	} else if ((ARCH_SUN4C_SUN4 && addr < 0xe0000000 &&
297		    addr + new_len > 0x20000000) ||
298		   addr + new_len > TASK_SIZE - PAGE_SIZE) {
299		unsigned long map_flags = 0;
300		struct file *file = NULL;
301
302		ret = -ENOMEM;
303		if (!(flags & MREMAP_MAYMOVE))
304			goto out_sem;
305
306		vma = find_vma(current->mm, addr);
307		if (vma) {
308			if (vma->vm_flags & VM_SHARED)
309				map_flags |= MAP_SHARED;
310			file = vma->vm_file;
311		}
312
313		new_addr = get_unmapped_area(file, addr, new_len,
314				     vma ? vma->vm_pgoff : 0,
315				     map_flags);
316		ret = new_addr;
317		if (new_addr & ~PAGE_MASK)
318			goto out_sem;
319		flags |= MREMAP_FIXED;
320	}
321	ret = do_mremap(addr, old_len, new_len, flags, new_addr);
322out_sem:
323	up_write(&current->mm->mmap_sem);
324out:
325	return ret;
326}
327
328/* we come to here via sys_nis_syscall so it can setup the regs argument */
329asmlinkage unsigned long
330c_sys_nis_syscall (struct pt_regs *regs)
331{
332	static int count = 0;
333
334	if (count++ > 5) return -ENOSYS;
335	printk ("%s[%d]: Unimplemented SPARC system call %d\n", current->comm, current->pid, (int)regs->u_regs[1]);
336#ifdef DEBUG_UNIMP_SYSCALL
337	show_regs (regs);
338#endif
339	return -ENOSYS;
340}
341
342/* #define DEBUG_SPARC_BREAKPOINT */
343
344asmlinkage void
345sparc_breakpoint (struct pt_regs *regs)
346{
347	siginfo_t info;
348
349	lock_kernel();
350#ifdef DEBUG_SPARC_BREAKPOINT
351        printk ("TRAP: Entering kernel PC=%x, nPC=%x\n", regs->pc, regs->npc);
352#endif
353	info.si_signo = SIGTRAP;
354	info.si_errno = 0;
355	info.si_code = TRAP_BRKPT;
356	info.si_addr = (void *)regs->pc;
357	info.si_trapno = 0;
358	force_sig_info(SIGTRAP, &info, current);
359
360#ifdef DEBUG_SPARC_BREAKPOINT
361	printk ("TRAP: Returning to space: PC=%x nPC=%x\n", regs->pc, regs->npc);
362#endif
363	unlock_kernel();
364}
365
366asmlinkage int
367sparc_sigaction (int sig, const struct old_sigaction *act,
368		 struct old_sigaction *oact)
369{
370	struct k_sigaction new_ka, old_ka;
371	int ret;
372
373	if (sig < 0) {
374		current->thread.new_signal = 1;
375		sig = -sig;
376	}
377
378	if (act) {
379		unsigned long mask;
380
381		if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
382		    __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
383		    __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
384			return -EFAULT;
385		__get_user(new_ka.sa.sa_flags, &act->sa_flags);
386		__get_user(mask, &act->sa_mask);
387		siginitset(&new_ka.sa.sa_mask, mask);
388		new_ka.ka_restorer = NULL;
389	}
390
391	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
392
393	if (!ret && oact) {
394		/* In the clone() case we could copy half consistant
395		 * state to the user, however this could sleep and
396		 * deadlock us if we held the signal lock on SMP.  So for
397		 * now I take the easy way out and do no locking.
398		 */
399		if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
400		    __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
401		    __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
402			return -EFAULT;
403		__put_user(old_ka.sa.sa_flags, &oact->sa_flags);
404		__put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
405	}
406
407	return ret;
408}
409
410asmlinkage int
411sys_rt_sigaction(int sig, const struct sigaction *act, struct sigaction *oact,
412		 void *restorer, size_t sigsetsize)
413{
414	struct k_sigaction new_ka, old_ka;
415	int ret;
416
417	if (sigsetsize != sizeof(sigset_t))
418		return -EINVAL;
419
420	/* All tasks which use RT signals (effectively) use
421	 * new style signals.
422	 */
423	current->thread.new_signal = 1;
424
425	if (act) {
426		new_ka.ka_restorer = restorer;
427		if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
428			return -EFAULT;
429	}
430
431	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
432
433	if (!ret && oact) {
434		if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
435			return -EFAULT;
436	}
437
438	return ret;
439}
440
441/* Just in case some old old binary calls this. */
442asmlinkage int sys_pause(void)
443{
444	current->state = TASK_INTERRUPTIBLE;
445	schedule();
446	return -ERESTARTNOHAND;
447}
448
449asmlinkage int sys_getdomainname(char *name, int len)
450{
451 	int nlen;
452 	int err = -EFAULT;
453
454 	down_read(&uts_sem);
455
456	nlen = strlen(system_utsname.domainname) + 1;
457
458	if (nlen < len)
459		len = nlen;
460	if(len > __NEW_UTS_LEN)
461		goto done;
462	if(copy_to_user(name, system_utsname.domainname, len))
463		goto done;
464	err = 0;
465done:
466	up_read(&uts_sem);
467	return err;
468}
469