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