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