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  */
sys_getpagesize(void)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 
arch_get_unmapped_area(struct file * filp,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)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 
sparc_brk(unsigned long brk)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  */
sparc_pipe(struct pt_regs * regs)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 
sys_ipc(uint call,int first,int second,int third,void * ptr,long fifth)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_semtimedop (first, (struct sembuf *)ptr, second, NULL);
127 			goto out;
128 		case SEMTIMEDOP:
129 			err = sys_semtimedop (first, (struct sembuf *)ptr, second, (const struct timespec *) fifth);
130 			goto out;
131 		case SEMGET:
132 			err = sys_semget (first, second, third);
133 			goto out;
134 		case SEMCTL: {
135 			union semun fourth;
136 			err = -EINVAL;
137 			if (!ptr)
138 				goto out;
139 			err = -EFAULT;
140 			if(get_user(fourth.__pad, (void **)ptr))
141 				goto out;
142 			err = sys_semctl (first, second, third, fourth);
143 			goto out;
144 			}
145 		default:
146 			err = -ENOSYS;
147 			goto out;
148 		}
149 	if (call <= MSGCTL)
150 		switch (call) {
151 		case MSGSND:
152 			err = sys_msgsnd (first, (struct msgbuf *) ptr,
153 					  second, third);
154 			goto out;
155 		case MSGRCV:
156 			switch (version) {
157 			case 0: {
158 				struct ipc_kludge tmp;
159 				err = -EINVAL;
160 				if (!ptr)
161 					goto out;
162 				err = -EFAULT;
163 				if(copy_from_user(&tmp,(struct ipc_kludge *) ptr, sizeof (tmp)))
164 					goto out;
165 				err = sys_msgrcv (first, tmp.msgp, second, tmp.msgtyp, third);
166 				goto out;
167 				}
168 			case 1: default:
169 				err = sys_msgrcv (first, (struct msgbuf *) ptr, second, fifth, third);
170 				goto out;
171 			}
172 		case MSGGET:
173 			err = sys_msgget ((key_t) first, second);
174 			goto out;
175 		case MSGCTL:
176 			err = sys_msgctl (first, second, (struct msqid_ds *) ptr);
177 			goto out;
178 		default:
179 			err = -ENOSYS;
180 			goto out;
181 		}
182 	if (call <= SHMCTL)
183 		switch (call) {
184 		case SHMAT:
185 			switch (version) {
186 			case 0: default: {
187 				ulong raddr;
188 				err = sys_shmat (first, (char *) ptr, second, &raddr);
189 				if (err)
190 					goto out;
191 				err = -EFAULT;
192 				if(put_user (raddr, (ulong *) third))
193 					goto out;
194 				err = 0;
195 				goto out;
196 				}
197 			case 1:	/* iBCS2 emulator entry point */
198 				err = sys_shmat (first, (char *) ptr, second, (ulong *) third);
199 				goto out;
200 			}
201 		case SHMDT:
202 			err = sys_shmdt ((char *)ptr);
203 			goto out;
204 		case SHMGET:
205 			err = sys_shmget (first, second, third);
206 			goto out;
207 		case SHMCTL:
208 			err = sys_shmctl (first, second, (struct shmid_ds *) ptr);
209 			goto out;
210 		default:
211 			err = -ENOSYS;
212 			goto out;
213 		}
214 	else
215 		err = -ENOSYS;
216 out:
217 	return err;
218 }
219 
220 /* Linux version of mmap */
do_mmap2(unsigned long addr,unsigned long len,unsigned long prot,unsigned long flags,unsigned long fd,unsigned long pgoff)221 static unsigned long do_mmap2(unsigned long addr, unsigned long len,
222 	unsigned long prot, unsigned long flags, unsigned long fd,
223 	unsigned long pgoff)
224 {
225 	struct file * file = NULL;
226 	unsigned long retval = -EBADF;
227 
228 	if (!(flags & MAP_ANONYMOUS)) {
229 		file = fget(fd);
230 		if (!file)
231 			goto out;
232 	}
233 
234 	retval = -EINVAL;
235 	len = PAGE_ALIGN(len);
236 	if (ARCH_SUN4C_SUN4 &&
237 	    (len > 0x20000000 ||
238 	     (addr < 0xe0000000 && addr + len > 0x20000000)))
239 		goto out_putf;
240 
241 	/* See asm-sparc/uaccess.h */
242 	if (len > TASK_SIZE - PAGE_SIZE || addr + len > TASK_SIZE - PAGE_SIZE)
243 		goto out_putf;
244 
245 	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
246 
247 	down_write(&current->mm->mmap_sem);
248 	retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
249 	up_write(&current->mm->mmap_sem);
250 
251 out_putf:
252 	if (file)
253 		fput(file);
254 out:
255 	return retval;
256 }
257 
sys_mmap2(unsigned long addr,unsigned long len,unsigned long prot,unsigned long flags,unsigned long fd,unsigned long pgoff)258 asmlinkage unsigned long sys_mmap2(unsigned long addr, unsigned long len,
259 	unsigned long prot, unsigned long flags, unsigned long fd,
260 	unsigned long pgoff)
261 {
262 	/* Make sure the shift for mmap2 is constant (12), no matter what PAGE_SIZE
263 	   we have. */
264 	return do_mmap2(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT - 12));
265 }
266 
sys_mmap(unsigned long addr,unsigned long len,unsigned long prot,unsigned long flags,unsigned long fd,unsigned long off)267 asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
268 	unsigned long prot, unsigned long flags, unsigned long fd,
269 	unsigned long off)
270 {
271 	return do_mmap2(addr, len, prot, flags, fd, off >> PAGE_SHIFT);
272 }
273 
274 extern unsigned long do_mremap(unsigned long addr,
275 	unsigned long old_len, unsigned long new_len,
276 	unsigned long flags, unsigned long new_addr);
277 
sparc_mremap(unsigned long addr,unsigned long old_len,unsigned long new_len,unsigned long flags,unsigned long new_addr)278 asmlinkage unsigned long sparc_mremap(unsigned long addr,
279 	unsigned long old_len, unsigned long new_len,
280 	unsigned long flags, unsigned long new_addr)
281 {
282 	struct vm_area_struct *vma;
283 	unsigned long ret = -EINVAL;
284 	if (ARCH_SUN4C_SUN4) {
285 		if (old_len > 0x20000000 || new_len > 0x20000000)
286 			goto out;
287 		if (addr < 0xe0000000 && addr + old_len > 0x20000000)
288 			goto out;
289 	}
290 	if (old_len > TASK_SIZE - PAGE_SIZE ||
291 	    new_len > TASK_SIZE - PAGE_SIZE)
292 		goto out;
293 	down_write(&current->mm->mmap_sem);
294 	if (flags & MREMAP_FIXED) {
295 		if (ARCH_SUN4C_SUN4 &&
296 		    new_addr < 0xe0000000 &&
297 		    new_addr + new_len > 0x20000000)
298 			goto out_sem;
299 		if (new_addr + new_len > TASK_SIZE - PAGE_SIZE)
300 			goto out_sem;
301 	} else if ((ARCH_SUN4C_SUN4 && addr < 0xe0000000 &&
302 		    addr + new_len > 0x20000000) ||
303 		   addr + new_len > TASK_SIZE - PAGE_SIZE) {
304 		unsigned long map_flags = 0;
305 		struct file *file = NULL;
306 
307 		ret = -ENOMEM;
308 		if (!(flags & MREMAP_MAYMOVE))
309 			goto out_sem;
310 
311 		vma = find_vma(current->mm, addr);
312 		if (vma) {
313 			if (vma->vm_flags & VM_SHARED)
314 				map_flags |= MAP_SHARED;
315 			file = vma->vm_file;
316 		}
317 
318 		new_addr = get_unmapped_area(file, addr, new_len,
319 				     vma ? vma->vm_pgoff : 0,
320 				     map_flags);
321 		ret = new_addr;
322 		if (new_addr & ~PAGE_MASK)
323 			goto out_sem;
324 		flags |= MREMAP_FIXED;
325 	}
326 	ret = do_mremap(addr, old_len, new_len, flags, new_addr);
327 out_sem:
328 	up_write(&current->mm->mmap_sem);
329 out:
330 	return ret;
331 }
332 
333 /* we come to here via sys_nis_syscall so it can setup the regs argument */
334 asmlinkage unsigned long
c_sys_nis_syscall(struct pt_regs * regs)335 c_sys_nis_syscall (struct pt_regs *regs)
336 {
337 	static int count = 0;
338 
339 	if (count++ > 5) return -ENOSYS;
340 	printk ("%s[%d]: Unimplemented SPARC system call %d\n", current->comm, current->pid, (int)regs->u_regs[1]);
341 #ifdef DEBUG_UNIMP_SYSCALL
342 	show_regs (regs);
343 #endif
344 	return -ENOSYS;
345 }
346 
347 /* #define DEBUG_SPARC_BREAKPOINT */
348 
349 asmlinkage void
sparc_breakpoint(struct pt_regs * regs)350 sparc_breakpoint (struct pt_regs *regs)
351 {
352 	siginfo_t info;
353 
354 	lock_kernel();
355 #ifdef DEBUG_SPARC_BREAKPOINT
356         printk ("TRAP: Entering kernel PC=%x, nPC=%x\n", regs->pc, regs->npc);
357 #endif
358 	info.si_signo = SIGTRAP;
359 	info.si_errno = 0;
360 	info.si_code = TRAP_BRKPT;
361 	info.si_addr = (void *)regs->pc;
362 	info.si_trapno = 0;
363 	force_sig_info(SIGTRAP, &info, current);
364 
365 #ifdef DEBUG_SPARC_BREAKPOINT
366 	printk ("TRAP: Returning to space: PC=%x nPC=%x\n", regs->pc, regs->npc);
367 #endif
368 	unlock_kernel();
369 }
370 
371 asmlinkage int
sparc_sigaction(int sig,const struct old_sigaction * act,struct old_sigaction * oact)372 sparc_sigaction (int sig, const struct old_sigaction *act,
373 		 struct old_sigaction *oact)
374 {
375 	struct k_sigaction new_ka, old_ka;
376 	int ret;
377 
378 	if (sig < 0) {
379 		current->thread.new_signal = 1;
380 		sig = -sig;
381 	}
382 
383 	if (act) {
384 		unsigned long mask;
385 
386 		if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
387 		    __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
388 		    __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
389 			return -EFAULT;
390 		__get_user(new_ka.sa.sa_flags, &act->sa_flags);
391 		__get_user(mask, &act->sa_mask);
392 		siginitset(&new_ka.sa.sa_mask, mask);
393 		new_ka.ka_restorer = NULL;
394 	}
395 
396 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
397 
398 	if (!ret && oact) {
399 		/* In the clone() case we could copy half consistant
400 		 * state to the user, however this could sleep and
401 		 * deadlock us if we held the signal lock on SMP.  So for
402 		 * now I take the easy way out and do no locking.
403 		 */
404 		if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
405 		    __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
406 		    __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
407 			return -EFAULT;
408 		__put_user(old_ka.sa.sa_flags, &oact->sa_flags);
409 		__put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
410 	}
411 
412 	return ret;
413 }
414 
415 asmlinkage int
sys_rt_sigaction(int sig,const struct sigaction * act,struct sigaction * oact,void * restorer,size_t sigsetsize)416 sys_rt_sigaction(int sig, const struct sigaction *act, struct sigaction *oact,
417 		 void *restorer, size_t sigsetsize)
418 {
419 	struct k_sigaction new_ka, old_ka;
420 	int ret;
421 
422 	/* XXX: Don't preclude handling different sized sigset_t's.  */
423 	if (sigsetsize != sizeof(sigset_t))
424 		return -EINVAL;
425 
426 	/* All tasks which use RT signals (effectively) use
427 	 * new style signals.
428 	 */
429 	current->thread.new_signal = 1;
430 
431 	if (act) {
432 		new_ka.ka_restorer = restorer;
433 		if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
434 			return -EFAULT;
435 	}
436 
437 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
438 
439 	if (!ret && oact) {
440 		if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
441 			return -EFAULT;
442 	}
443 
444 	return ret;
445 }
446 
447 /* Just in case some old old binary calls this. */
sys_pause(void)448 asmlinkage int sys_pause(void)
449 {
450 	current->state = TASK_INTERRUPTIBLE;
451 	schedule();
452 	return -ERESTARTNOHAND;
453 }
454 
sys_getdomainname(char * name,int len)455 asmlinkage int sys_getdomainname(char *name, int len)
456 {
457  	int nlen;
458  	int err = -EFAULT;
459 
460  	down_read(&uts_sem);
461 
462 	nlen = strlen(system_utsname.domainname) + 1;
463 
464 	if (nlen < len)
465 		len = nlen;
466 	if(len > __NEW_UTS_LEN)
467 		goto done;
468 	if(copy_to_user(name, system_utsname.domainname, len))
469 		goto done;
470 	err = 0;
471 done:
472 	up_read(&uts_sem);
473 	return err;
474 }
475