Merge branch 'misc-3.2' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux
[pandora-kernel.git] / arch / alpha / kernel / osf_sys.c
1 /*
2  *  linux/arch/alpha/kernel/osf_sys.c
3  *
4  *  Copyright (C) 1995  Linus Torvalds
5  */
6
7 /*
8  * This file handles some of the stranger OSF/1 system call interfaces.
9  * Some of the system calls expect a non-C calling standard, others have
10  * special parameter blocks..
11  */
12
13 #include <linux/errno.h>
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
16 #include <linux/mm.h>
17 #include <linux/smp.h>
18 #include <linux/stddef.h>
19 #include <linux/syscalls.h>
20 #include <linux/unistd.h>
21 #include <linux/ptrace.h>
22 #include <linux/user.h>
23 #include <linux/utsname.h>
24 #include <linux/time.h>
25 #include <linux/timex.h>
26 #include <linux/major.h>
27 #include <linux/stat.h>
28 #include <linux/mman.h>
29 #include <linux/shm.h>
30 #include <linux/poll.h>
31 #include <linux/file.h>
32 #include <linux/types.h>
33 #include <linux/ipc.h>
34 #include <linux/namei.h>
35 #include <linux/uio.h>
36 #include <linux/vfs.h>
37 #include <linux/rcupdate.h>
38 #include <linux/slab.h>
39
40 #include <asm/fpu.h>
41 #include <asm/io.h>
42 #include <asm/uaccess.h>
43 #include <asm/system.h>
44 #include <asm/sysinfo.h>
45 #include <asm/thread_info.h>
46 #include <asm/hwrpb.h>
47 #include <asm/processor.h>
48
49 /*
50  * Brk needs to return an error.  Still support Linux's brk(0) query idiom,
51  * which OSF programs just shouldn't be doing.  We're still not quite
52  * identical to OSF as we don't return 0 on success, but doing otherwise
53  * would require changes to libc.  Hopefully this is good enough.
54  */
55 SYSCALL_DEFINE1(osf_brk, unsigned long, brk)
56 {
57         unsigned long retval = sys_brk(brk);
58         if (brk && brk != retval)
59                 retval = -ENOMEM;
60         return retval;
61 }
62  
63 /*
64  * This is pure guess-work..
65  */
66 SYSCALL_DEFINE4(osf_set_program_attributes, unsigned long, text_start,
67                 unsigned long, text_len, unsigned long, bss_start,
68                 unsigned long, bss_len)
69 {
70         struct mm_struct *mm;
71
72         mm = current->mm;
73         mm->end_code = bss_start + bss_len;
74         mm->start_brk = bss_start + bss_len;
75         mm->brk = bss_start + bss_len;
76 #if 0
77         printk("set_program_attributes(%lx %lx %lx %lx)\n",
78                 text_start, text_len, bss_start, bss_len);
79 #endif
80         return 0;
81 }
82
83 /*
84  * OSF/1 directory handling functions...
85  *
86  * The "getdents()" interface is much more sane: the "basep" stuff is
87  * braindamage (it can't really handle filesystems where the directory
88  * offset differences aren't the same as "d_reclen").
89  */
90 #define NAME_OFFSET     offsetof (struct osf_dirent, d_name)
91
92 struct osf_dirent {
93         unsigned int d_ino;
94         unsigned short d_reclen;
95         unsigned short d_namlen;
96         char d_name[1];
97 };
98
99 struct osf_dirent_callback {
100         struct osf_dirent __user *dirent;
101         long __user *basep;
102         unsigned int count;
103         int error;
104 };
105
106 static int
107 osf_filldir(void *__buf, const char *name, int namlen, loff_t offset,
108             u64 ino, unsigned int d_type)
109 {
110         struct osf_dirent __user *dirent;
111         struct osf_dirent_callback *buf = (struct osf_dirent_callback *) __buf;
112         unsigned int reclen = ALIGN(NAME_OFFSET + namlen + 1, sizeof(u32));
113         unsigned int d_ino;
114
115         buf->error = -EINVAL;   /* only used if we fail */
116         if (reclen > buf->count)
117                 return -EINVAL;
118         d_ino = ino;
119         if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
120                 buf->error = -EOVERFLOW;
121                 return -EOVERFLOW;
122         }
123         if (buf->basep) {
124                 if (put_user(offset, buf->basep))
125                         goto Efault;
126                 buf->basep = NULL;
127         }
128         dirent = buf->dirent;
129         if (put_user(d_ino, &dirent->d_ino) ||
130             put_user(namlen, &dirent->d_namlen) ||
131             put_user(reclen, &dirent->d_reclen) ||
132             copy_to_user(dirent->d_name, name, namlen) ||
133             put_user(0, dirent->d_name + namlen))
134                 goto Efault;
135         dirent = (void __user *)dirent + reclen;
136         buf->dirent = dirent;
137         buf->count -= reclen;
138         return 0;
139 Efault:
140         buf->error = -EFAULT;
141         return -EFAULT;
142 }
143
144 SYSCALL_DEFINE4(osf_getdirentries, unsigned int, fd,
145                 struct osf_dirent __user *, dirent, unsigned int, count,
146                 long __user *, basep)
147 {
148         int error;
149         struct file *file;
150         struct osf_dirent_callback buf;
151
152         error = -EBADF;
153         file = fget(fd);
154         if (!file)
155                 goto out;
156
157         buf.dirent = dirent;
158         buf.basep = basep;
159         buf.count = count;
160         buf.error = 0;
161
162         error = vfs_readdir(file, osf_filldir, &buf);
163         if (error >= 0)
164                 error = buf.error;
165         if (count != buf.count)
166                 error = count - buf.count;
167
168         fput(file);
169  out:
170         return error;
171 }
172
173 #undef NAME_OFFSET
174
175 SYSCALL_DEFINE6(osf_mmap, unsigned long, addr, unsigned long, len,
176                 unsigned long, prot, unsigned long, flags, unsigned long, fd,
177                 unsigned long, off)
178 {
179         unsigned long ret = -EINVAL;
180
181 #if 0
182         if (flags & (_MAP_HASSEMAPHORE | _MAP_INHERIT | _MAP_UNALIGNED))
183                 printk("%s: unimplemented OSF mmap flags %04lx\n", 
184                         current->comm, flags);
185 #endif
186         if ((off + PAGE_ALIGN(len)) < off)
187                 goto out;
188         if (off & ~PAGE_MASK)
189                 goto out;
190         ret = sys_mmap_pgoff(addr, len, prot, flags, fd, off >> PAGE_SHIFT);
191  out:
192         return ret;
193 }
194
195
196 /*
197  * The OSF/1 statfs structure is much larger, but this should
198  * match the beginning, at least.
199  */
200 struct osf_statfs {
201         short f_type;
202         short f_flags;
203         int f_fsize;
204         int f_bsize;
205         int f_blocks;
206         int f_bfree;
207         int f_bavail;
208         int f_files;
209         int f_ffree;
210         __kernel_fsid_t f_fsid;
211 };
212
213 static int
214 linux_to_osf_statfs(struct kstatfs *linux_stat, struct osf_statfs __user *osf_stat,
215                     unsigned long bufsiz)
216 {
217         struct osf_statfs tmp_stat;
218
219         tmp_stat.f_type = linux_stat->f_type;
220         tmp_stat.f_flags = 0;   /* mount flags */
221         tmp_stat.f_fsize = linux_stat->f_frsize;
222         tmp_stat.f_bsize = linux_stat->f_bsize;
223         tmp_stat.f_blocks = linux_stat->f_blocks;
224         tmp_stat.f_bfree = linux_stat->f_bfree;
225         tmp_stat.f_bavail = linux_stat->f_bavail;
226         tmp_stat.f_files = linux_stat->f_files;
227         tmp_stat.f_ffree = linux_stat->f_ffree;
228         tmp_stat.f_fsid = linux_stat->f_fsid;
229         if (bufsiz > sizeof(tmp_stat))
230                 bufsiz = sizeof(tmp_stat);
231         return copy_to_user(osf_stat, &tmp_stat, bufsiz) ? -EFAULT : 0;
232 }
233
234 SYSCALL_DEFINE3(osf_statfs, const char __user *, pathname,
235                 struct osf_statfs __user *, buffer, unsigned long, bufsiz)
236 {
237         struct kstatfs linux_stat;
238         int error = user_statfs(pathname, &linux_stat);
239         if (!error)
240                 error = linux_to_osf_statfs(&linux_stat, buffer, bufsiz);
241         return error;   
242 }
243
244 SYSCALL_DEFINE3(osf_fstatfs, unsigned long, fd,
245                 struct osf_statfs __user *, buffer, unsigned long, bufsiz)
246 {
247         struct kstatfs linux_stat;
248         int error = fd_statfs(fd, &linux_stat);
249         if (!error)
250                 error = linux_to_osf_statfs(&linux_stat, buffer, bufsiz);
251         return error;
252 }
253
254 /*
255  * Uhh.. OSF/1 mount parameters aren't exactly obvious..
256  *
257  * Although to be frank, neither are the native Linux/i386 ones..
258  */
259 struct ufs_args {
260         char __user *devname;
261         int flags;
262         uid_t exroot;
263 };
264
265 struct cdfs_args {
266         char __user *devname;
267         int flags;
268         uid_t exroot;
269
270         /* This has lots more here, which Linux handles with the option block
271            but I'm too lazy to do the translation into ASCII.  */
272 };
273
274 struct procfs_args {
275         char __user *devname;
276         int flags;
277         uid_t exroot;
278 };
279
280 /*
281  * We can't actually handle ufs yet, so we translate UFS mounts to
282  * ext2fs mounts. I wouldn't mind a UFS filesystem, but the UFS
283  * layout is so braindead it's a major headache doing it.
284  *
285  * Just how long ago was it written? OTOH our UFS driver may be still
286  * unhappy with OSF UFS. [CHECKME]
287  */
288 static int
289 osf_ufs_mount(char *dirname, struct ufs_args __user *args, int flags)
290 {
291         int retval;
292         struct cdfs_args tmp;
293         char *devname;
294
295         retval = -EFAULT;
296         if (copy_from_user(&tmp, args, sizeof(tmp)))
297                 goto out;
298         devname = getname(tmp.devname);
299         retval = PTR_ERR(devname);
300         if (IS_ERR(devname))
301                 goto out;
302         retval = do_mount(devname, dirname, "ext2", flags, NULL);
303         putname(devname);
304  out:
305         return retval;
306 }
307
308 static int
309 osf_cdfs_mount(char *dirname, struct cdfs_args __user *args, int flags)
310 {
311         int retval;
312         struct cdfs_args tmp;
313         char *devname;
314
315         retval = -EFAULT;
316         if (copy_from_user(&tmp, args, sizeof(tmp)))
317                 goto out;
318         devname = getname(tmp.devname);
319         retval = PTR_ERR(devname);
320         if (IS_ERR(devname))
321                 goto out;
322         retval = do_mount(devname, dirname, "iso9660", flags, NULL);
323         putname(devname);
324  out:
325         return retval;
326 }
327
328 static int
329 osf_procfs_mount(char *dirname, struct procfs_args __user *args, int flags)
330 {
331         struct procfs_args tmp;
332
333         if (copy_from_user(&tmp, args, sizeof(tmp)))
334                 return -EFAULT;
335
336         return do_mount("", dirname, "proc", flags, NULL);
337 }
338
339 SYSCALL_DEFINE4(osf_mount, unsigned long, typenr, const char __user *, path,
340                 int, flag, void __user *, data)
341 {
342         int retval;
343         char *name;
344
345         name = getname(path);
346         retval = PTR_ERR(name);
347         if (IS_ERR(name))
348                 goto out;
349         switch (typenr) {
350         case 1:
351                 retval = osf_ufs_mount(name, data, flag);
352                 break;
353         case 6:
354                 retval = osf_cdfs_mount(name, data, flag);
355                 break;
356         case 9:
357                 retval = osf_procfs_mount(name, data, flag);
358                 break;
359         default:
360                 retval = -EINVAL;
361                 printk("osf_mount(%ld, %x)\n", typenr, flag);
362         }
363         putname(name);
364  out:
365         return retval;
366 }
367
368 SYSCALL_DEFINE1(osf_utsname, char __user *, name)
369 {
370         int error;
371
372         down_read(&uts_sem);
373         error = -EFAULT;
374         if (copy_to_user(name + 0, utsname()->sysname, 32))
375                 goto out;
376         if (copy_to_user(name + 32, utsname()->nodename, 32))
377                 goto out;
378         if (copy_to_user(name + 64, utsname()->release, 32))
379                 goto out;
380         if (copy_to_user(name + 96, utsname()->version, 32))
381                 goto out;
382         if (copy_to_user(name + 128, utsname()->machine, 32))
383                 goto out;
384
385         error = 0;
386  out:
387         up_read(&uts_sem);      
388         return error;
389 }
390
391 SYSCALL_DEFINE0(getpagesize)
392 {
393         return PAGE_SIZE;
394 }
395
396 SYSCALL_DEFINE0(getdtablesize)
397 {
398         return sysctl_nr_open;
399 }
400
401 /*
402  * For compatibility with OSF/1 only.  Use utsname(2) instead.
403  */
404 SYSCALL_DEFINE2(osf_getdomainname, char __user *, name, int, namelen)
405 {
406         unsigned len;
407         int i;
408
409         if (!access_ok(VERIFY_WRITE, name, namelen))
410                 return -EFAULT;
411
412         len = namelen;
413         if (len > 32)
414                 len = 32;
415
416         down_read(&uts_sem);
417         for (i = 0; i < len; ++i) {
418                 __put_user(utsname()->domainname[i], name + i);
419                 if (utsname()->domainname[i] == '\0')
420                         break;
421         }
422         up_read(&uts_sem);
423
424         return 0;
425 }
426
427 /*
428  * The following stuff should move into a header file should it ever
429  * be labeled "officially supported."  Right now, there is just enough
430  * support to avoid applications (such as tar) printing error
431  * messages.  The attributes are not really implemented.
432  */
433
434 /*
435  * Values for Property list entry flag
436  */
437 #define PLE_PROPAGATE_ON_COPY           0x1     /* cp(1) will copy entry
438                                                    by default */
439 #define PLE_FLAG_MASK                   0x1     /* Valid flag values */
440 #define PLE_FLAG_ALL                    -1      /* All flag value */
441
442 struct proplistname_args {
443         unsigned int pl_mask;
444         unsigned int pl_numnames;
445         char **pl_names;
446 };
447
448 union pl_args {
449         struct setargs {
450                 char __user *path;
451                 long follow;
452                 long nbytes;
453                 char __user *buf;
454         } set;
455         struct fsetargs {
456                 long fd;
457                 long nbytes;
458                 char __user *buf;
459         } fset;
460         struct getargs {
461                 char __user *path;
462                 long follow;
463                 struct proplistname_args __user *name_args;
464                 long nbytes;
465                 char __user *buf;
466                 int __user *min_buf_size;
467         } get;
468         struct fgetargs {
469                 long fd;
470                 struct proplistname_args __user *name_args;
471                 long nbytes;
472                 char __user *buf;
473                 int __user *min_buf_size;
474         } fget;
475         struct delargs {
476                 char __user *path;
477                 long follow;
478                 struct proplistname_args __user *name_args;
479         } del;
480         struct fdelargs {
481                 long fd;
482                 struct proplistname_args __user *name_args;
483         } fdel;
484 };
485
486 enum pl_code {
487         PL_SET = 1, PL_FSET = 2,
488         PL_GET = 3, PL_FGET = 4,
489         PL_DEL = 5, PL_FDEL = 6
490 };
491
492 SYSCALL_DEFINE2(osf_proplist_syscall, enum pl_code, code,
493                 union pl_args __user *, args)
494 {
495         long error;
496         int __user *min_buf_size_ptr;
497
498         switch (code) {
499         case PL_SET:
500                 if (get_user(error, &args->set.nbytes))
501                         error = -EFAULT;
502                 break;
503         case PL_FSET:
504                 if (get_user(error, &args->fset.nbytes))
505                         error = -EFAULT;
506                 break;
507         case PL_GET:
508                 error = get_user(min_buf_size_ptr, &args->get.min_buf_size);
509                 if (error)
510                         break;
511                 error = put_user(0, min_buf_size_ptr);
512                 break;
513         case PL_FGET:
514                 error = get_user(min_buf_size_ptr, &args->fget.min_buf_size);
515                 if (error)
516                         break;
517                 error = put_user(0, min_buf_size_ptr);
518                 break;
519         case PL_DEL:
520         case PL_FDEL:
521                 error = 0;
522                 break;
523         default:
524                 error = -EOPNOTSUPP;
525                 break;
526         };
527         return error;
528 }
529
530 SYSCALL_DEFINE2(osf_sigstack, struct sigstack __user *, uss,
531                 struct sigstack __user *, uoss)
532 {
533         unsigned long usp = rdusp();
534         unsigned long oss_sp = current->sas_ss_sp + current->sas_ss_size;
535         unsigned long oss_os = on_sig_stack(usp);
536         int error;
537
538         if (uss) {
539                 void __user *ss_sp;
540
541                 error = -EFAULT;
542                 if (get_user(ss_sp, &uss->ss_sp))
543                         goto out;
544
545                 /* If the current stack was set with sigaltstack, don't
546                    swap stacks while we are on it.  */
547                 error = -EPERM;
548                 if (current->sas_ss_sp && on_sig_stack(usp))
549                         goto out;
550
551                 /* Since we don't know the extent of the stack, and we don't
552                    track onstack-ness, but rather calculate it, we must 
553                    presume a size.  Ho hum this interface is lossy.  */
554                 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
555                 current->sas_ss_size = SIGSTKSZ;
556         }
557
558         if (uoss) {
559                 error = -EFAULT;
560                 if (! access_ok(VERIFY_WRITE, uoss, sizeof(*uoss))
561                     || __put_user(oss_sp, &uoss->ss_sp)
562                     || __put_user(oss_os, &uoss->ss_onstack))
563                         goto out;
564         }
565
566         error = 0;
567  out:
568         return error;
569 }
570
571 SYSCALL_DEFINE3(osf_sysinfo, int, command, char __user *, buf, long, count)
572 {
573         const char *sysinfo_table[] = {
574                 utsname()->sysname,
575                 utsname()->nodename,
576                 utsname()->release,
577                 utsname()->version,
578                 utsname()->machine,
579                 "alpha",        /* instruction set architecture */
580                 "dummy",        /* hardware serial number */
581                 "dummy",        /* hardware manufacturer */
582                 "dummy",        /* secure RPC domain */
583         };
584         unsigned long offset;
585         const char *res;
586         long len, err = -EINVAL;
587
588         offset = command-1;
589         if (offset >= ARRAY_SIZE(sysinfo_table)) {
590                 /* Digital UNIX has a few unpublished interfaces here */
591                 printk("sysinfo(%d)", command);
592                 goto out;
593         }
594
595         down_read(&uts_sem);
596         res = sysinfo_table[offset];
597         len = strlen(res)+1;
598         if ((unsigned long)len > (unsigned long)count)
599                 len = count;
600         if (copy_to_user(buf, res, len))
601                 err = -EFAULT;
602         else
603                 err = 0;
604         up_read(&uts_sem);
605  out:
606         return err;
607 }
608
609 SYSCALL_DEFINE5(osf_getsysinfo, unsigned long, op, void __user *, buffer,
610                 unsigned long, nbytes, int __user *, start, void __user *, arg)
611 {
612         unsigned long w;
613         struct percpu_struct *cpu;
614
615         switch (op) {
616         case GSI_IEEE_FP_CONTROL:
617                 /* Return current software fp control & status bits.  */
618                 /* Note that DU doesn't verify available space here.  */
619
620                 w = current_thread_info()->ieee_state & IEEE_SW_MASK;
621                 w = swcr_update_status(w, rdfpcr());
622                 if (put_user(w, (unsigned long __user *) buffer))
623                         return -EFAULT;
624                 return 0;
625
626         case GSI_IEEE_STATE_AT_SIGNAL:
627                 /*
628                  * Not sure anybody will ever use this weird stuff.  These
629                  * ops can be used (under OSF/1) to set the fpcr that should
630                  * be used when a signal handler starts executing.
631                  */
632                 break;
633
634         case GSI_UACPROC:
635                 if (nbytes < sizeof(unsigned int))
636                         return -EINVAL;
637                 w = (current_thread_info()->flags >> ALPHA_UAC_SHIFT) &
638                         UAC_BITMASK;
639                 if (put_user(w, (unsigned int __user *)buffer))
640                         return -EFAULT;
641                 return 1;
642
643         case GSI_PROC_TYPE:
644                 if (nbytes < sizeof(unsigned long))
645                         return -EINVAL;
646                 cpu = (struct percpu_struct*)
647                   ((char*)hwrpb + hwrpb->processor_offset);
648                 w = cpu->type;
649                 if (put_user(w, (unsigned long  __user*)buffer))
650                         return -EFAULT;
651                 return 1;
652
653         case GSI_GET_HWRPB:
654                 if (nbytes > sizeof(*hwrpb))
655                         return -EINVAL;
656                 if (copy_to_user(buffer, hwrpb, nbytes) != 0)
657                         return -EFAULT;
658                 return 1;
659
660         default:
661                 break;
662         }
663
664         return -EOPNOTSUPP;
665 }
666
667 SYSCALL_DEFINE5(osf_setsysinfo, unsigned long, op, void __user *, buffer,
668                 unsigned long, nbytes, int __user *, start, void __user *, arg)
669 {
670         switch (op) {
671         case SSI_IEEE_FP_CONTROL: {
672                 unsigned long swcr, fpcr;
673                 unsigned int *state;
674
675                 /* 
676                  * Alpha Architecture Handbook 4.7.7.3:
677                  * To be fully IEEE compiant, we must track the current IEEE
678                  * exception state in software, because spurious bits can be
679                  * set in the trap shadow of a software-complete insn.
680                  */
681
682                 if (get_user(swcr, (unsigned long __user *)buffer))
683                         return -EFAULT;
684                 state = &current_thread_info()->ieee_state;
685
686                 /* Update softare trap enable bits.  */
687                 *state = (*state & ~IEEE_SW_MASK) | (swcr & IEEE_SW_MASK);
688
689                 /* Update the real fpcr.  */
690                 fpcr = rdfpcr() & FPCR_DYN_MASK;
691                 fpcr |= ieee_swcr_to_fpcr(swcr);
692                 wrfpcr(fpcr);
693
694                 return 0;
695         }
696
697         case SSI_IEEE_RAISE_EXCEPTION: {
698                 unsigned long exc, swcr, fpcr, fex;
699                 unsigned int *state;
700
701                 if (get_user(exc, (unsigned long __user *)buffer))
702                         return -EFAULT;
703                 state = &current_thread_info()->ieee_state;
704                 exc &= IEEE_STATUS_MASK;
705
706                 /* Update softare trap enable bits.  */
707                 swcr = (*state & IEEE_SW_MASK) | exc;
708                 *state |= exc;
709
710                 /* Update the real fpcr.  */
711                 fpcr = rdfpcr();
712                 fpcr |= ieee_swcr_to_fpcr(swcr);
713                 wrfpcr(fpcr);
714
715                 /* If any exceptions set by this call, and are unmasked,
716                    send a signal.  Old exceptions are not signaled.  */
717                 fex = (exc >> IEEE_STATUS_TO_EXCSUM_SHIFT) & swcr;
718                 if (fex) {
719                         siginfo_t info;
720                         int si_code = 0;
721
722                         if (fex & IEEE_TRAP_ENABLE_DNO) si_code = FPE_FLTUND;
723                         if (fex & IEEE_TRAP_ENABLE_INE) si_code = FPE_FLTRES;
724                         if (fex & IEEE_TRAP_ENABLE_UNF) si_code = FPE_FLTUND;
725                         if (fex & IEEE_TRAP_ENABLE_OVF) si_code = FPE_FLTOVF;
726                         if (fex & IEEE_TRAP_ENABLE_DZE) si_code = FPE_FLTDIV;
727                         if (fex & IEEE_TRAP_ENABLE_INV) si_code = FPE_FLTINV;
728
729                         info.si_signo = SIGFPE;
730                         info.si_errno = 0;
731                         info.si_code = si_code;
732                         info.si_addr = NULL;  /* FIXME */
733                         send_sig_info(SIGFPE, &info, current);
734                 }
735                 return 0;
736         }
737
738         case SSI_IEEE_STATE_AT_SIGNAL:
739         case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
740                 /*
741                  * Not sure anybody will ever use this weird stuff.  These
742                  * ops can be used (under OSF/1) to set the fpcr that should
743                  * be used when a signal handler starts executing.
744                  */
745                 break;
746
747         case SSI_NVPAIRS: {
748                 unsigned long v, w, i;
749                 unsigned int old, new;
750                 
751                 for (i = 0; i < nbytes; ++i) {
752
753                         if (get_user(v, 2*i + (unsigned int __user *)buffer))
754                                 return -EFAULT;
755                         if (get_user(w, 2*i + 1 + (unsigned int __user *)buffer))
756                                 return -EFAULT;
757                         switch (v) {
758                         case SSIN_UACPROC:
759                         again:
760                                 old = current_thread_info()->flags;
761                                 new = old & ~(UAC_BITMASK << ALPHA_UAC_SHIFT);
762                                 new = new | (w & UAC_BITMASK) << ALPHA_UAC_SHIFT;
763                                 if (cmpxchg(&current_thread_info()->flags,
764                                             old, new) != old)
765                                         goto again;
766                                 break;
767  
768                         default:
769                                 return -EOPNOTSUPP;
770                         }
771                 }
772                 return 0;
773         }
774  
775         default:
776                 break;
777         }
778
779         return -EOPNOTSUPP;
780 }
781
782 /* Translations due to the fact that OSF's time_t is an int.  Which
783    affects all sorts of things, like timeval and itimerval.  */
784
785 extern struct timezone sys_tz;
786
787 struct timeval32
788 {
789     int tv_sec, tv_usec;
790 };
791
792 struct itimerval32
793 {
794     struct timeval32 it_interval;
795     struct timeval32 it_value;
796 };
797
798 static inline long
799 get_tv32(struct timeval *o, struct timeval32 __user *i)
800 {
801         return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
802                 (__get_user(o->tv_sec, &i->tv_sec) |
803                  __get_user(o->tv_usec, &i->tv_usec)));
804 }
805
806 static inline long
807 put_tv32(struct timeval32 __user *o, struct timeval *i)
808 {
809         return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
810                 (__put_user(i->tv_sec, &o->tv_sec) |
811                  __put_user(i->tv_usec, &o->tv_usec)));
812 }
813
814 static inline long
815 get_it32(struct itimerval *o, struct itimerval32 __user *i)
816 {
817         return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
818                 (__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) |
819                  __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) |
820                  __get_user(o->it_value.tv_sec, &i->it_value.tv_sec) |
821                  __get_user(o->it_value.tv_usec, &i->it_value.tv_usec)));
822 }
823
824 static inline long
825 put_it32(struct itimerval32 __user *o, struct itimerval *i)
826 {
827         return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
828                 (__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) |
829                  __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) |
830                  __put_user(i->it_value.tv_sec, &o->it_value.tv_sec) |
831                  __put_user(i->it_value.tv_usec, &o->it_value.tv_usec)));
832 }
833
834 static inline void
835 jiffies_to_timeval32(unsigned long jiffies, struct timeval32 *value)
836 {
837         value->tv_usec = (jiffies % HZ) * (1000000L / HZ);
838         value->tv_sec = jiffies / HZ;
839 }
840
841 SYSCALL_DEFINE2(osf_gettimeofday, struct timeval32 __user *, tv,
842                 struct timezone __user *, tz)
843 {
844         if (tv) {
845                 struct timeval ktv;
846                 do_gettimeofday(&ktv);
847                 if (put_tv32(tv, &ktv))
848                         return -EFAULT;
849         }
850         if (tz) {
851                 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
852                         return -EFAULT;
853         }
854         return 0;
855 }
856
857 SYSCALL_DEFINE2(osf_settimeofday, struct timeval32 __user *, tv,
858                 struct timezone __user *, tz)
859 {
860         struct timespec kts;
861         struct timezone ktz;
862
863         if (tv) {
864                 if (get_tv32((struct timeval *)&kts, tv))
865                         return -EFAULT;
866         }
867         if (tz) {
868                 if (copy_from_user(&ktz, tz, sizeof(*tz)))
869                         return -EFAULT;
870         }
871
872         kts.tv_nsec *= 1000;
873
874         return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
875 }
876
877 SYSCALL_DEFINE2(osf_getitimer, int, which, struct itimerval32 __user *, it)
878 {
879         struct itimerval kit;
880         int error;
881
882         error = do_getitimer(which, &kit);
883         if (!error && put_it32(it, &kit))
884                 error = -EFAULT;
885
886         return error;
887 }
888
889 SYSCALL_DEFINE3(osf_setitimer, int, which, struct itimerval32 __user *, in,
890                 struct itimerval32 __user *, out)
891 {
892         struct itimerval kin, kout;
893         int error;
894
895         if (in) {
896                 if (get_it32(&kin, in))
897                         return -EFAULT;
898         } else
899                 memset(&kin, 0, sizeof(kin));
900
901         error = do_setitimer(which, &kin, out ? &kout : NULL);
902         if (error || !out)
903                 return error;
904
905         if (put_it32(out, &kout))
906                 return -EFAULT;
907
908         return 0;
909
910 }
911
912 SYSCALL_DEFINE2(osf_utimes, const char __user *, filename,
913                 struct timeval32 __user *, tvs)
914 {
915         struct timespec tv[2];
916
917         if (tvs) {
918                 struct timeval ktvs[2];
919                 if (get_tv32(&ktvs[0], &tvs[0]) ||
920                     get_tv32(&ktvs[1], &tvs[1]))
921                         return -EFAULT;
922
923                 if (ktvs[0].tv_usec < 0 || ktvs[0].tv_usec >= 1000000 ||
924                     ktvs[1].tv_usec < 0 || ktvs[1].tv_usec >= 1000000)
925                         return -EINVAL;
926
927                 tv[0].tv_sec = ktvs[0].tv_sec;
928                 tv[0].tv_nsec = 1000 * ktvs[0].tv_usec;
929                 tv[1].tv_sec = ktvs[1].tv_sec;
930                 tv[1].tv_nsec = 1000 * ktvs[1].tv_usec;
931         }
932
933         return do_utimes(AT_FDCWD, filename, tvs ? tv : NULL, 0);
934 }
935
936 SYSCALL_DEFINE5(osf_select, int, n, fd_set __user *, inp, fd_set __user *, outp,
937                 fd_set __user *, exp, struct timeval32 __user *, tvp)
938 {
939         struct timespec end_time, *to = NULL;
940         if (tvp) {
941                 time_t sec, usec;
942
943                 to = &end_time;
944
945                 if (!access_ok(VERIFY_READ, tvp, sizeof(*tvp))
946                     || __get_user(sec, &tvp->tv_sec)
947                     || __get_user(usec, &tvp->tv_usec)) {
948                         return -EFAULT;
949                 }
950
951                 if (sec < 0 || usec < 0)
952                         return -EINVAL;
953
954                 if (poll_select_set_timeout(to, sec, usec * NSEC_PER_USEC))
955                         return -EINVAL;         
956
957         }
958
959         /* OSF does not copy back the remaining time.  */
960         return core_sys_select(n, inp, outp, exp, to);
961 }
962
963 struct rusage32 {
964         struct timeval32 ru_utime;      /* user time used */
965         struct timeval32 ru_stime;      /* system time used */
966         long    ru_maxrss;              /* maximum resident set size */
967         long    ru_ixrss;               /* integral shared memory size */
968         long    ru_idrss;               /* integral unshared data size */
969         long    ru_isrss;               /* integral unshared stack size */
970         long    ru_minflt;              /* page reclaims */
971         long    ru_majflt;              /* page faults */
972         long    ru_nswap;               /* swaps */
973         long    ru_inblock;             /* block input operations */
974         long    ru_oublock;             /* block output operations */
975         long    ru_msgsnd;              /* messages sent */
976         long    ru_msgrcv;              /* messages received */
977         long    ru_nsignals;            /* signals received */
978         long    ru_nvcsw;               /* voluntary context switches */
979         long    ru_nivcsw;              /* involuntary " */
980 };
981
982 SYSCALL_DEFINE2(osf_getrusage, int, who, struct rusage32 __user *, ru)
983 {
984         struct rusage32 r;
985
986         if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
987                 return -EINVAL;
988
989         memset(&r, 0, sizeof(r));
990         switch (who) {
991         case RUSAGE_SELF:
992                 jiffies_to_timeval32(current->utime, &r.ru_utime);
993                 jiffies_to_timeval32(current->stime, &r.ru_stime);
994                 r.ru_minflt = current->min_flt;
995                 r.ru_majflt = current->maj_flt;
996                 break;
997         case RUSAGE_CHILDREN:
998                 jiffies_to_timeval32(current->signal->cutime, &r.ru_utime);
999                 jiffies_to_timeval32(current->signal->cstime, &r.ru_stime);
1000                 r.ru_minflt = current->signal->cmin_flt;
1001                 r.ru_majflt = current->signal->cmaj_flt;
1002                 break;
1003         }
1004
1005         return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1006 }
1007
1008 SYSCALL_DEFINE4(osf_wait4, pid_t, pid, int __user *, ustatus, int, options,
1009                 struct rusage32 __user *, ur)
1010 {
1011         struct rusage r;
1012         long ret, err;
1013         unsigned int status = 0;
1014         mm_segment_t old_fs;
1015
1016         if (!ur)
1017                 return sys_wait4(pid, ustatus, options, NULL);
1018
1019         old_fs = get_fs();
1020                 
1021         set_fs (KERNEL_DS);
1022         ret = sys_wait4(pid, (unsigned int __user *) &status, options,
1023                         (struct rusage __user *) &r);
1024         set_fs (old_fs);
1025
1026         if (!access_ok(VERIFY_WRITE, ur, sizeof(*ur)))
1027                 return -EFAULT;
1028
1029         err = 0;
1030         err |= put_user(status, ustatus);
1031         err |= __put_user(r.ru_utime.tv_sec, &ur->ru_utime.tv_sec);
1032         err |= __put_user(r.ru_utime.tv_usec, &ur->ru_utime.tv_usec);
1033         err |= __put_user(r.ru_stime.tv_sec, &ur->ru_stime.tv_sec);
1034         err |= __put_user(r.ru_stime.tv_usec, &ur->ru_stime.tv_usec);
1035         err |= __put_user(r.ru_maxrss, &ur->ru_maxrss);
1036         err |= __put_user(r.ru_ixrss, &ur->ru_ixrss);
1037         err |= __put_user(r.ru_idrss, &ur->ru_idrss);
1038         err |= __put_user(r.ru_isrss, &ur->ru_isrss);
1039         err |= __put_user(r.ru_minflt, &ur->ru_minflt);
1040         err |= __put_user(r.ru_majflt, &ur->ru_majflt);
1041         err |= __put_user(r.ru_nswap, &ur->ru_nswap);
1042         err |= __put_user(r.ru_inblock, &ur->ru_inblock);
1043         err |= __put_user(r.ru_oublock, &ur->ru_oublock);
1044         err |= __put_user(r.ru_msgsnd, &ur->ru_msgsnd);
1045         err |= __put_user(r.ru_msgrcv, &ur->ru_msgrcv);
1046         err |= __put_user(r.ru_nsignals, &ur->ru_nsignals);
1047         err |= __put_user(r.ru_nvcsw, &ur->ru_nvcsw);
1048         err |= __put_user(r.ru_nivcsw, &ur->ru_nivcsw);
1049
1050         return err ? err : ret;
1051 }
1052
1053 /*
1054  * I don't know what the parameters are: the first one
1055  * seems to be a timeval pointer, and I suspect the second
1056  * one is the time remaining.. Ho humm.. No documentation.
1057  */
1058 SYSCALL_DEFINE2(osf_usleep_thread, struct timeval32 __user *, sleep,
1059                 struct timeval32 __user *, remain)
1060 {
1061         struct timeval tmp;
1062         unsigned long ticks;
1063
1064         if (get_tv32(&tmp, sleep))
1065                 goto fault;
1066
1067         ticks = timeval_to_jiffies(&tmp);
1068
1069         ticks = schedule_timeout_interruptible(ticks);
1070
1071         if (remain) {
1072                 jiffies_to_timeval(ticks, &tmp);
1073                 if (put_tv32(remain, &tmp))
1074                         goto fault;
1075         }
1076         
1077         return 0;
1078  fault:
1079         return -EFAULT;
1080 }
1081
1082
1083 struct timex32 {
1084         unsigned int modes;     /* mode selector */
1085         long offset;            /* time offset (usec) */
1086         long freq;              /* frequency offset (scaled ppm) */
1087         long maxerror;          /* maximum error (usec) */
1088         long esterror;          /* estimated error (usec) */
1089         int status;             /* clock command/status */
1090         long constant;          /* pll time constant */
1091         long precision;         /* clock precision (usec) (read only) */
1092         long tolerance;         /* clock frequency tolerance (ppm)
1093                                  * (read only)
1094                                  */
1095         struct timeval32 time;  /* (read only) */
1096         long tick;              /* (modified) usecs between clock ticks */
1097
1098         long ppsfreq;           /* pps frequency (scaled ppm) (ro) */
1099         long jitter;            /* pps jitter (us) (ro) */
1100         int shift;              /* interval duration (s) (shift) (ro) */
1101         long stabil;            /* pps stability (scaled ppm) (ro) */
1102         long jitcnt;            /* jitter limit exceeded (ro) */
1103         long calcnt;            /* calibration intervals (ro) */
1104         long errcnt;            /* calibration errors (ro) */
1105         long stbcnt;            /* stability limit exceeded (ro) */
1106
1107         int  :32; int  :32; int  :32; int  :32;
1108         int  :32; int  :32; int  :32; int  :32;
1109         int  :32; int  :32; int  :32; int  :32;
1110 };
1111
1112 SYSCALL_DEFINE1(old_adjtimex, struct timex32 __user *, txc_p)
1113 {
1114         struct timex txc;
1115         int ret;
1116
1117         /* copy relevant bits of struct timex. */
1118         if (copy_from_user(&txc, txc_p, offsetof(struct timex32, time)) ||
1119             copy_from_user(&txc.tick, &txc_p->tick, sizeof(struct timex32) - 
1120                            offsetof(struct timex32, time)))
1121           return -EFAULT;
1122
1123         ret = do_adjtimex(&txc);        
1124         if (ret < 0)
1125           return ret;
1126         
1127         /* copy back to timex32 */
1128         if (copy_to_user(txc_p, &txc, offsetof(struct timex32, time)) ||
1129             (copy_to_user(&txc_p->tick, &txc.tick, sizeof(struct timex32) - 
1130                           offsetof(struct timex32, tick))) ||
1131             (put_tv32(&txc_p->time, &txc.time)))
1132           return -EFAULT;
1133
1134         return ret;
1135 }
1136
1137 /* Get an address range which is currently unmapped.  Similar to the
1138    generic version except that we know how to honor ADDR_LIMIT_32BIT.  */
1139
1140 static unsigned long
1141 arch_get_unmapped_area_1(unsigned long addr, unsigned long len,
1142                          unsigned long limit)
1143 {
1144         struct vm_area_struct *vma = find_vma(current->mm, addr);
1145
1146         while (1) {
1147                 /* At this point:  (!vma || addr < vma->vm_end). */
1148                 if (limit - len < addr)
1149                         return -ENOMEM;
1150                 if (!vma || addr + len <= vma->vm_start)
1151                         return addr;
1152                 addr = vma->vm_end;
1153                 vma = vma->vm_next;
1154         }
1155 }
1156
1157 unsigned long
1158 arch_get_unmapped_area(struct file *filp, unsigned long addr,
1159                        unsigned long len, unsigned long pgoff,
1160                        unsigned long flags)
1161 {
1162         unsigned long limit;
1163
1164         /* "32 bit" actually means 31 bit, since pointers sign extend.  */
1165         if (current->personality & ADDR_LIMIT_32BIT)
1166                 limit = 0x80000000;
1167         else
1168                 limit = TASK_SIZE;
1169
1170         if (len > limit)
1171                 return -ENOMEM;
1172
1173         if (flags & MAP_FIXED)
1174                 return addr;
1175
1176         /* First, see if the given suggestion fits.
1177
1178            The OSF/1 loader (/sbin/loader) relies on us returning an
1179            address larger than the requested if one exists, which is
1180            a terribly broken way to program.
1181
1182            That said, I can see the use in being able to suggest not
1183            merely specific addresses, but regions of memory -- perhaps
1184            this feature should be incorporated into all ports?  */
1185
1186         if (addr) {
1187                 addr = arch_get_unmapped_area_1 (PAGE_ALIGN(addr), len, limit);
1188                 if (addr != (unsigned long) -ENOMEM)
1189                         return addr;
1190         }
1191
1192         /* Next, try allocating at TASK_UNMAPPED_BASE.  */
1193         addr = arch_get_unmapped_area_1 (PAGE_ALIGN(TASK_UNMAPPED_BASE),
1194                                          len, limit);
1195         if (addr != (unsigned long) -ENOMEM)
1196                 return addr;
1197
1198         /* Finally, try allocating in low memory.  */
1199         addr = arch_get_unmapped_area_1 (PAGE_SIZE, len, limit);
1200
1201         return addr;
1202 }
1203
1204 #ifdef CONFIG_OSF4_COMPAT
1205
1206 /* Clear top 32 bits of iov_len in the user's buffer for
1207    compatibility with old versions of OSF/1 where iov_len
1208    was defined as int. */
1209 static int
1210 osf_fix_iov_len(const struct iovec __user *iov, unsigned long count)
1211 {
1212         unsigned long i;
1213
1214         for (i = 0 ; i < count ; i++) {
1215                 int __user *iov_len_high = (int __user *)&iov[i].iov_len + 1;
1216
1217                 if (put_user(0, iov_len_high))
1218                         return -EFAULT;
1219         }
1220         return 0;
1221 }
1222
1223 SYSCALL_DEFINE3(osf_readv, unsigned long, fd,
1224                 const struct iovec __user *, vector, unsigned long, count)
1225 {
1226         if (unlikely(personality(current->personality) == PER_OSF4))
1227                 if (osf_fix_iov_len(vector, count))
1228                         return -EFAULT;
1229         return sys_readv(fd, vector, count);
1230 }
1231
1232 SYSCALL_DEFINE3(osf_writev, unsigned long, fd,
1233                 const struct iovec __user *, vector, unsigned long, count)
1234 {
1235         if (unlikely(personality(current->personality) == PER_OSF4))
1236                 if (osf_fix_iov_len(vector, count))
1237                         return -EFAULT;
1238         return sys_writev(fd, vector, count);
1239 }
1240
1241 #endif