File: /usr/src/linux/arch/sparc/kernel/sys_sparc.c

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