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