Import 2.3.47pre1
[davej-history.git] / arch / sparc64 / kernel / sys_sparc.c
blob81b4c4de11663a131169d314534781bee173168c
1 /* $Id: sys_sparc.c,v 1.36 2000/02/16 07:31:35 davem Exp $
2 * linux/arch/sparc64/kernel/sys_sparc.c
4 * This file contains various random system calls that
5 * have a non-standard calling sequence on the Linux/sparc
6 * platform.
7 */
9 #include <linux/config.h>
10 #include <linux/errno.h>
11 #include <linux/types.h>
12 #include <linux/sched.h>
13 #include <linux/fs.h>
14 #include <linux/file.h>
15 #include <linux/mm.h>
16 #include <linux/sem.h>
17 #include <linux/msg.h>
18 #include <linux/shm.h>
19 #include <linux/stat.h>
20 #include <linux/mman.h>
21 #include <linux/utsname.h>
22 #include <linux/smp.h>
23 #include <linux/smp_lock.h>
24 #include <linux/malloc.h>
25 #include <linux/ipc.h>
27 #include <asm/uaccess.h>
28 #include <asm/ipc.h>
29 #include <asm/utrap.h>
30 #include <asm/perfctr.h>
32 /* #define DEBUG_UNIMP_SYSCALL */
34 /* XXX Make this per-binary type, this way we can detect the type of
35 * XXX a binary. Every Sparc executable calls this very early on.
37 asmlinkage unsigned long sys_getpagesize(void)
39 return PAGE_SIZE;
42 unsigned long get_unmapped_area(unsigned long addr, unsigned long len)
44 struct vm_area_struct * vmm;
45 unsigned long task_size = TASK_SIZE;
47 if (current->thread.flags & SPARC_FLAG_32BIT)
48 task_size = 0xf0000000UL;
49 if (len > task_size || len > -PAGE_OFFSET)
50 return 0;
51 if (!addr)
52 addr = TASK_UNMAPPED_BASE;
53 addr = PAGE_ALIGN(addr);
55 task_size -= len;
57 for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
58 /* At this point: (!vmm || addr < vmm->vm_end). */
59 if (addr < PAGE_OFFSET && -PAGE_OFFSET - len < addr) {
60 addr = PAGE_OFFSET;
61 vmm = find_vma(current->mm, PAGE_OFFSET);
63 if (task_size < addr)
64 return 0;
65 if (!vmm || addr + len <= vmm->vm_start)
66 return addr;
67 addr = vmm->vm_end;
71 extern asmlinkage unsigned long sys_brk(unsigned long brk);
73 asmlinkage unsigned long sparc_brk(unsigned long brk)
75 /* People could try to be nasty and use ta 0x6d in 32bit programs */
76 if ((current->thread.flags & SPARC_FLAG_32BIT) &&
77 brk >= 0xf0000000UL)
78 return current->mm->brk;
80 if ((current->mm->brk & PAGE_OFFSET) != (brk & PAGE_OFFSET))
81 return current->mm->brk;
82 return sys_brk(brk);
86 * sys_pipe() is the normal C calling standard for creating
87 * a pipe. It's not the way unix traditionally does this, though.
89 asmlinkage int sparc_pipe(struct pt_regs *regs)
91 int fd[2];
92 int error;
94 lock_kernel();
95 error = do_pipe(fd);
96 if (error)
97 goto out;
98 regs->u_regs[UREG_I1] = fd[1];
99 error = fd[0];
100 out:
101 unlock_kernel();
102 return error;
106 * sys_ipc() is the de-multiplexer for the SysV IPC calls..
108 * This is really horribly ugly.
111 asmlinkage int sys_ipc (unsigned call, int first, int second, unsigned long third, void *ptr, long fifth)
113 int err;
115 lock_kernel();
116 /* No need for backward compatibility. We can start fresh... */
118 if (call <= SEMCTL)
119 switch (call) {
120 case SEMOP:
121 err = sys_semop (first, (struct sembuf *)ptr, second);
122 goto out;
123 case SEMGET:
124 err = sys_semget (first, second, (int)third);
125 goto out;
126 case SEMCTL: {
127 union semun fourth;
128 err = -EINVAL;
129 if (!ptr)
130 goto out;
131 err = -EFAULT;
132 if(get_user(fourth.__pad, (void **)ptr))
133 goto out;
134 err = sys_semctl (first, second | IPC_64, (int)third, fourth);
135 goto out;
137 default:
138 err = -EINVAL;
139 goto out;
141 if (call <= MSGCTL)
142 switch (call) {
143 case MSGSND:
144 err = sys_msgsnd (first, (struct msgbuf *) ptr,
145 second, (int)third);
146 goto out;
147 case MSGRCV:
148 err = sys_msgrcv (first, (struct msgbuf *) ptr, second, fifth, (int)third);
149 goto out;
150 case MSGGET:
151 err = sys_msgget ((key_t) first, second);
152 goto out;
153 case MSGCTL:
154 err = sys_msgctl (first, second | IPC_64, (struct msqid_ds *) ptr);
155 goto out;
156 default:
157 err = -EINVAL;
158 goto out;
160 if (call <= SHMCTL)
161 switch (call) {
162 case SHMAT:
163 err = sys_shmat (first, (char *) ptr, second, (ulong *) third);
164 goto out;
165 case SHMDT:
166 err = sys_shmdt ((char *)ptr);
167 goto out;
168 case SHMGET:
169 err = sys_shmget (first, second, (int)third);
170 goto out;
171 case SHMCTL:
172 err = sys_shmctl (first, second | IPC_64, (struct shmid_ds *) ptr);
173 goto out;
174 default:
175 err = -EINVAL;
176 goto out;
178 else
179 err = -EINVAL;
180 out:
181 unlock_kernel();
182 return err;
185 /* Linux version of mmap */
186 asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
187 unsigned long prot, unsigned long flags, unsigned long fd,
188 unsigned long off)
190 struct file * file = NULL;
191 unsigned long retval = -EBADF;
193 if (!(flags & MAP_ANONYMOUS)) {
194 file = fget(fd);
195 if (!file)
196 goto out;
198 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
199 len = PAGE_ALIGN(len);
200 retval = -EINVAL;
202 down(&current->mm->mmap_sem);
203 lock_kernel();
205 if (current->thread.flags & SPARC_FLAG_32BIT) {
206 if (len > 0xf0000000UL ||
207 ((flags & MAP_FIXED) && addr > 0xf0000000UL - len))
208 goto out_putf;
209 } else {
210 if (len > -PAGE_OFFSET ||
211 ((flags & MAP_FIXED) &&
212 addr < PAGE_OFFSET && addr + len > -PAGE_OFFSET))
213 goto out_putf;
216 retval = do_mmap(file, addr, len, prot, flags, off);
218 out_putf:
219 unlock_kernel();
220 up(&current->mm->mmap_sem);
221 if (file)
222 fput(file);
223 out:
224 return retval;
227 asmlinkage long sys64_munmap(unsigned long addr, size_t len)
229 long ret;
231 if (len > -PAGE_OFFSET ||
232 (addr < PAGE_OFFSET && addr + len > -PAGE_OFFSET))
233 return -EINVAL;
234 down(&current->mm->mmap_sem);
235 ret = do_munmap(addr, len);
236 up(&current->mm->mmap_sem);
237 return ret;
240 extern unsigned long do_mremap(unsigned long addr,
241 unsigned long old_len, unsigned long new_len,
242 unsigned long flags, unsigned long new_addr);
244 asmlinkage unsigned long sys64_mremap(unsigned long addr,
245 unsigned long old_len, unsigned long new_len,
246 unsigned long flags, unsigned long new_addr)
248 unsigned long ret = -EINVAL;
249 if (current->thread.flags & SPARC_FLAG_32BIT)
250 goto out;
251 if (old_len > -PAGE_OFFSET || new_len > -PAGE_OFFSET)
252 goto out;
253 if (addr < PAGE_OFFSET && addr + old_len > -PAGE_OFFSET)
254 goto out;
255 down(&current->mm->mmap_sem);
256 if (flags & MREMAP_FIXED) {
257 if (new_addr < PAGE_OFFSET &&
258 new_addr + new_len > -PAGE_OFFSET)
259 goto out_sem;
260 } else if (addr < PAGE_OFFSET && addr + new_len > -PAGE_OFFSET) {
261 ret = -ENOMEM;
262 if (!(flags & MREMAP_MAYMOVE))
263 goto out_sem;
264 new_addr = get_unmapped_area (addr, new_len);
265 if (!new_addr)
266 goto out_sem;
267 flags |= MREMAP_FIXED;
269 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
270 out_sem:
271 up(&current->mm->mmap_sem);
272 out:
273 return ret;
276 /* we come to here via sys_nis_syscall so it can setup the regs argument */
277 asmlinkage unsigned long
278 c_sys_nis_syscall (struct pt_regs *regs)
280 static int count=0;
282 /* Don't make the system unusable, if someone goes stuck */
283 if (count++ > 5) return -ENOSYS;
284 lock_kernel();
285 printk ("Unimplemented SPARC system call %ld\n",regs->u_regs[1]);
286 #ifdef DEBUG_UNIMP_SYSCALL
287 show_regs (regs);
288 #endif
289 unlock_kernel();
290 return -ENOSYS;
293 /* #define DEBUG_SPARC_BREAKPOINT */
295 asmlinkage void
296 sparc_breakpoint (struct pt_regs *regs)
298 siginfo_t info;
300 lock_kernel();
301 #ifdef DEBUG_SPARC_BREAKPOINT
302 printk ("TRAP: Entering kernel PC=%lx, nPC=%lx\n", regs->tpc, regs->tnpc);
303 #endif
304 info.si_signo = SIGTRAP;
305 info.si_errno = 0;
306 info.si_code = TRAP_BRKPT;
307 info.si_addr = (void *)regs->tpc;
308 info.si_trapno = 0;
309 force_sig_info(SIGTRAP, &info, current);
310 #ifdef DEBUG_SPARC_BREAKPOINT
311 printk ("TRAP: Returning to space: PC=%lx nPC=%lx\n", regs->tpc, regs->tnpc);
312 #endif
313 unlock_kernel();
316 extern void check_pending(int signum);
318 asmlinkage int sys_getdomainname(char *name, int len)
320 int nlen;
321 int err = -EFAULT;
323 down_read(&uts_sem);
325 nlen = strlen(system_utsname.domainname) + 1;
327 if (nlen < len)
328 len = nlen;
329 if(len > __NEW_UTS_LEN)
330 goto done;
331 if(copy_to_user(name, system_utsname.domainname, len))
332 goto done;
333 err = 0;
334 done:
335 up_read(&uts_sem);
336 return err;
339 /* only AP+ systems have sys_aplib */
340 asmlinkage int sys_aplib(void)
342 return -ENOSYS;
345 asmlinkage int solaris_syscall(struct pt_regs *regs)
347 static int count = 0;
348 lock_kernel();
349 regs->tpc = regs->tnpc;
350 regs->tnpc += 4;
351 if(++count <= 20)
352 printk ("For Solaris binary emulation you need solaris module loaded\n");
353 show_regs (regs);
354 send_sig(SIGSEGV, current, 1);
355 unlock_kernel();
356 return -ENOSYS;
359 #ifndef CONFIG_SUNOS_EMUL
360 asmlinkage int sunos_syscall(struct pt_regs *regs)
362 static int count = 0;
363 lock_kernel();
364 regs->tpc = regs->tnpc;
365 regs->tnpc += 4;
366 if(++count <= 20)
367 printk ("SunOS binary emulation not compiled in\n");
368 force_sig(SIGSEGV, current);
369 unlock_kernel();
370 return -ENOSYS;
372 #endif
374 asmlinkage int sys_utrap_install(utrap_entry_t type, utrap_handler_t new_p,
375 utrap_handler_t new_d,
376 utrap_handler_t *old_p, utrap_handler_t *old_d)
378 if (type < UT_INSTRUCTION_EXCEPTION || type > UT_TRAP_INSTRUCTION_31)
379 return -EINVAL;
380 if (new_p == (utrap_handler_t)(long)UTH_NOCHANGE) {
381 if (old_p) {
382 if (!current->thread.utraps)
383 put_user_ret(NULL, old_p, -EFAULT);
384 else
385 put_user_ret((utrap_handler_t)(current->thread.utraps[type]), old_p, -EFAULT);
387 if (old_d)
388 put_user_ret(NULL, old_d, -EFAULT);
389 return 0;
391 lock_kernel();
392 if (!current->thread.utraps) {
393 current->thread.utraps = kmalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long), GFP_KERNEL);
394 if (!current->thread.utraps) return -ENOMEM;
395 current->thread.utraps[0] = 1;
396 memset(current->thread.utraps+1, 0, UT_TRAP_INSTRUCTION_31*sizeof(long));
397 } else {
398 if ((utrap_handler_t)current->thread.utraps[type] != new_p && current->thread.utraps[0] > 1) {
399 long *p = current->thread.utraps;
401 current->thread.utraps = kmalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long), GFP_KERNEL);
402 if (!current->thread.utraps) {
403 current->thread.utraps = p;
404 return -ENOMEM;
406 p[0]--;
407 current->thread.utraps[0] = 1;
408 memcpy(current->thread.utraps+1, p+1, UT_TRAP_INSTRUCTION_31*sizeof(long));
411 if (old_p)
412 put_user_ret((utrap_handler_t)(current->thread.utraps[type]), old_p, -EFAULT);
413 if (old_d)
414 put_user_ret(NULL, old_d, -EFAULT);
415 current->thread.utraps[type] = (long)new_p;
416 unlock_kernel();
417 return 0;
420 long sparc_memory_ordering(unsigned long model, struct pt_regs *regs)
422 if (model >= 3)
423 return -EINVAL;
424 regs->tstate = (regs->tstate & ~TSTATE_MM) | (model << 14);
425 return 0;
428 asmlinkage int
429 sys_rt_sigaction(int sig, const struct sigaction *act, struct sigaction *oact,
430 void *restorer, size_t sigsetsize)
432 struct k_sigaction new_ka, old_ka;
433 int ret;
435 /* XXX: Don't preclude handling different sized sigset_t's. */
436 if (sigsetsize != sizeof(sigset_t))
437 return -EINVAL;
439 if (act) {
440 new_ka.ka_restorer = restorer;
441 if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
442 return -EFAULT;
445 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
447 if (!ret && oact) {
448 if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
449 return -EFAULT;
452 return ret;
455 /* Invoked by rtrap code to update performance counters in
456 * user space.
458 asmlinkage void
459 update_perfctrs(void)
461 unsigned long pic, tmp;
463 read_pic(pic);
464 tmp = (current->thread.kernel_cntd0 += (unsigned int)pic);
465 __put_user(tmp, current->thread.user_cntd0);
466 tmp = (current->thread.kernel_cntd1 += (pic >> 32));
467 __put_user(tmp, current->thread.user_cntd1);
468 reset_pic();
471 asmlinkage int
472 sys_perfctr(int opcode, unsigned long arg0, unsigned long arg1, unsigned long arg2)
474 int err = 0;
476 switch(opcode) {
477 case PERFCTR_ON:
478 current->thread.pcr_reg = arg2;
479 current->thread.user_cntd0 = (u64 *) arg0;
480 current->thread.user_cntd1 = (u64 *) arg1;
481 current->thread.kernel_cntd0 =
482 current->thread.kernel_cntd1 = 0;
483 write_pcr(arg2);
484 reset_pic();
485 current->thread.flags |= SPARC_FLAG_PERFCTR;
486 break;
488 case PERFCTR_OFF:
489 err = -EINVAL;
490 if ((current->thread.flags & SPARC_FLAG_PERFCTR) != 0) {
491 current->thread.user_cntd0 =
492 current->thread.user_cntd1 = NULL;
493 current->thread.pcr_reg = 0;
494 write_pcr(0);
495 current->thread.flags &= ~(SPARC_FLAG_PERFCTR);
496 err = 0;
498 break;
500 case PERFCTR_READ: {
501 unsigned long pic, tmp;
503 if (!(current->thread.flags & SPARC_FLAG_PERFCTR)) {
504 err = -EINVAL;
505 break;
507 read_pic(pic);
508 tmp = (current->thread.kernel_cntd0 += (unsigned int)pic);
509 err |= __put_user(tmp, current->thread.user_cntd0);
510 tmp = (current->thread.kernel_cntd1 += (pic >> 32));
511 err |= __put_user(tmp, current->thread.user_cntd1);
512 reset_pic();
513 break;
516 case PERFCTR_CLRPIC:
517 if (!(current->thread.flags & SPARC_FLAG_PERFCTR)) {
518 err = -EINVAL;
519 break;
521 current->thread.kernel_cntd0 =
522 current->thread.kernel_cntd1 = 0;
523 reset_pic();
524 break;
526 case PERFCTR_SETPCR: {
527 u64 *user_pcr = (u64 *)arg0;
528 if (!(current->thread.flags & SPARC_FLAG_PERFCTR)) {
529 err = -EINVAL;
530 break;
532 err |= __get_user(current->thread.pcr_reg, user_pcr);
533 write_pcr(current->thread.pcr_reg);
534 current->thread.kernel_cntd0 =
535 current->thread.kernel_cntd1 = 0;
536 reset_pic();
537 break;
540 case PERFCTR_GETPCR: {
541 u64 *user_pcr = (u64 *)arg0;
542 if (!(current->thread.flags & SPARC_FLAG_PERFCTR)) {
543 err = -EINVAL;
544 break;
546 err |= __put_user(current->thread.pcr_reg, user_pcr);
547 break;
550 default:
551 err = -EINVAL;
552 break;
554 return err;