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