1/*	$NetBSD: linux_osf1.c,v 1.5 2021/09/07 11:43:03 riastradh Exp $	*/
2
3/*
4 * Copyright (c) 1999 Christopher G. Demetriou.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *      This product includes software developed by Christopher G. Demetriou
17 *	for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__KERNEL_RCSID(0, "$NetBSD: linux_osf1.c,v 1.5 2021/09/07 11:43:03 riastradh Exp $");
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/namei.h>
39#include <sys/proc.h>
40#include <sys/file.h>
41#include <sys/filedesc.h>
42#include <sys/kernel.h>
43#include <sys/mount.h>
44#include <sys/vnode.h>
45#include <sys/wait.h>
46#include <sys/select.h>
47#include <sys/syscallargs.h>
48#include <sys/vfs_syscalls.h>
49
50#include <machine/alpha.h>
51#include <machine/cpuconf.h>
52#include <machine/rpb.h>
53#include <machine/fpu.h>
54
55#include <compat/common/compat_util.h>
56#include <compat/linux/common/linux_types.h>
57#include <compat/linux/common/linux_signal.h>
58#include <compat/linux/arch/alpha/linux_signal.h>
59#include <compat/linux/arch/alpha/linux_osf1.h>
60#include <compat/linux/linux_syscallargs.h>
61
62#include <net/if.h>
63#include <netinet/in.h>
64
65#include <nfs/rpcv2.h>
66#include <nfs/nfsproto.h>
67#include <nfs/nfs.h>
68#include <nfs/nfsmount.h>
69
70#include <ufs/ufs/quota.h>
71#include <ufs/ufs/ufsmount.h>
72
73#include <machine/vmparam.h>
74
75const struct emul_flags_xtab osf1_wait_options_xtab[] = {
76    {	OSF1_WNOHANG,		OSF1_WNOHANG,		WNOHANG		},
77    {	OSF1_WUNTRACED,		OSF1_WUNTRACED,		WUNTRACED	},
78    {	0								}
79};
80
81const struct emul_flags_xtab osf1_nfs_mount_flags_xtab[] = {
82    {	OSF1_NFSMNT_SOFT,	OSF1_NFSMNT_SOFT,	NFSMNT_SOFT,	},
83    {	OSF1_NFSMNT_WSIZE,	OSF1_NFSMNT_WSIZE,	NFSMNT_WSIZE,	},
84    {	OSF1_NFSMNT_RSIZE,	OSF1_NFSMNT_RSIZE,	NFSMNT_RSIZE,	},
85    {	OSF1_NFSMNT_TIMEO,	OSF1_NFSMNT_TIMEO,	NFSMNT_TIMEO,	},
86    {	OSF1_NFSMNT_RETRANS,	OSF1_NFSMNT_RETRANS,	NFSMNT_RETRANS,	},
87#if 0 /* no equivalent; needs special handling, see below */
88    {	OSF1_NFSMNT_HOSTNAME,	OSF1_NFSMNT_HOSTNAME,	???,		},
89#endif
90    {	OSF1_NFSMNT_INT,	OSF1_NFSMNT_INT,	NFSMNT_INT,	},
91    {	OSF1_NFSMNT_NOCONN,	OSF1_NFSMNT_NOCONN,	NFSMNT_NOCONN,	},
92#if 0 /* no equivalents */
93    {	OSF1_NFSMNT_NOAC,	OSF1_NFSMNT_NOAC,	???,		},
94    {	OSF1_NFSMNT_ACREGMIN,	OSF1_NFSMNT_ACREGMIN,	???,		},
95    {	OSF1_NFSMNT_ACREGMAX,	OSF1_NFSMNT_ACREGMAX,	???,		},
96    {	OSF1_NFSMNT_ACDIRMIN,	OSF1_NFSMNT_ACDIRMIN,	???,		},
97    {	OSF1_NFSMNT_ACDIRMAX,	OSF1_NFSMNT_ACDIRMAX,	???,		},
98    {	OSF1_NFSMNT_NOCTO,	OSF1_NFSMNT_NOCTO,	???,		},
99    {	OSF1_NFSMNT_POSIX,	OSF1_NFSMNT_POSIX,	???,		},
100    {	OSF1_NFSMNT_AUTO,	OSF1_NFSMNT_AUTO,	???,		},
101    {	OSF1_NFSMNT_SEC,	OSF1_NFSMNT_SEC,	???,		},
102    {	OSF1_NFSMNT_TCP,	OSF1_NFSMNT_TCP,	???,		},
103    {	OSF1_NFSMNT_PROPLIST,	OSF1_NFSMNT_PROPLIST,	???,		},
104#endif
105    {	0								}
106};
107
108static void
109osf1_cvt_rusage_from_native(const struct rusage *ru, struct osf1_rusage *oru)
110{
111
112	memset(oru, 0, sizeof(*oru));
113
114	oru->ru_utime.tv_sec = ru->ru_utime.tv_sec;
115	oru->ru_utime.tv_usec = ru->ru_utime.tv_usec;
116
117	oru->ru_stime.tv_sec = ru->ru_stime.tv_sec;
118	oru->ru_stime.tv_usec = ru->ru_stime.tv_usec;
119
120	oru->ru_maxrss = ru->ru_maxrss;
121	oru->ru_ixrss = ru->ru_ixrss;
122	oru->ru_idrss = ru->ru_idrss;
123	oru->ru_isrss = ru->ru_isrss;
124	oru->ru_minflt = ru->ru_minflt;
125	oru->ru_majflt = ru->ru_majflt;
126	oru->ru_nswap = ru->ru_nswap;
127	oru->ru_inblock = ru->ru_inblock;
128	oru->ru_oublock = ru->ru_oublock;
129	oru->ru_msgsnd = ru->ru_msgsnd;
130	oru->ru_msgrcv = ru->ru_msgrcv;
131	oru->ru_nsignals = ru->ru_nsignals;
132	oru->ru_nvcsw = ru->ru_nvcsw;
133	oru->ru_nivcsw = ru->ru_nivcsw;
134}
135
136static void
137osf1_cvt_statfs_from_native(const struct statvfs *bsfs, struct osf1_statfs *osfs)
138{
139
140	memset(osfs, 0, sizeof(*osfs));
141	if (!strncmp(MOUNT_FFS, bsfs->f_fstypename, sizeof(bsfs->f_fstypename)))
142		osfs->f_type = OSF1_MOUNT_UFS;
143	else if (!strncmp(MOUNT_NFS, bsfs->f_fstypename, sizeof(bsfs->f_fstypename)))
144		osfs->f_type = OSF1_MOUNT_NFS;
145	else if (!strncmp(MOUNT_MFS, bsfs->f_fstypename, sizeof(bsfs->f_fstypename)))
146		osfs->f_type = OSF1_MOUNT_MFS;
147	else
148		/* uh oh...  XXX = PC, CDFS, PROCFS, etc. */
149		osfs->f_type = OSF1_MOUNT_ADDON;
150	osfs->f_flags = bsfs->f_flag;		/* XXX translate */
151	osfs->f_fsize = bsfs->f_frsize;
152	osfs->f_bsize = bsfs->f_bsize;
153	osfs->f_blocks = bsfs->f_blocks;
154	osfs->f_bfree = bsfs->f_bfree;
155	osfs->f_bavail = bsfs->f_bavail;
156	osfs->f_files = bsfs->f_files;
157	osfs->f_ffree = bsfs->f_ffree;
158	memcpy(&osfs->f_fsid, &bsfs->f_fsidx, sizeof osfs->f_fsid);
159	/* osfs->f_spare zeroed above */
160	memcpy(osfs->f_mntonname, bsfs->f_mntonname, sizeof osfs->f_mntonname);
161	memcpy(osfs->f_mntfromname, bsfs->f_mntfromname,
162	    sizeof osfs->f_mntfromname);
163	/* XXX osfs->f_xxx should be filled in... */
164}
165
166/* --------------------------------------------------------------------- */
167
168int
169linux_sys_osf1_wait4(struct lwp *l, const struct linux_sys_osf1_wait4_args *uap, register_t *retval)
170{
171	struct osf1_rusage osf1_rusage;
172	struct rusage netbsd_rusage;
173	unsigned long leftovers;
174	int error, status;
175	int options = SCARG(uap, options);
176	int pid = SCARG(uap, pid);
177
178	/* translate options */
179	options = emul_flags_translate(osf1_wait_options_xtab,
180	    options, &leftovers);
181	if (leftovers != 0)
182		return (EINVAL);
183
184	error = do_sys_wait(&pid, &status, options,
185	    SCARG(uap, rusage) != NULL ? &netbsd_rusage : NULL);
186
187	retval[0] = pid;
188	if (pid == 0)
189		return error;
190
191	if (SCARG(uap, rusage)) {
192		osf1_cvt_rusage_from_native(&netbsd_rusage, &osf1_rusage);
193		error = copyout(&osf1_rusage, SCARG(uap, rusage),
194		    sizeof osf1_rusage);
195	}
196
197	if (error == 0 && SCARG(uap, status))
198		error = copyout(&status, SCARG(uap, status), sizeof(status));
199
200	return error;
201}
202
203#define	OSF1_MNT_WAIT		0x1
204#define	OSF1_MNT_NOWAIT		0x2
205
206#define	OSF1_MNT_FORCE		0x1
207#define	OSF1_MNT_NOFORCE	0x2
208
209/* acceptable flags for various calls */
210#define	OSF1_GETFSSTAT_FLAGS	(OSF1_MNT_WAIT|OSF1_MNT_NOWAIT)
211#define	OSF1_MOUNT_FLAGS	0xffffffff			/* XXX */
212#define	OSF1_UNMOUNT_FLAGS	(OSF1_MNT_FORCE|OSF1_MNT_NOFORCE)
213
214static int
215osf1_mount_mfs(struct lwp *l, const struct linux_sys_osf1_mount_args *uap)
216{
217	struct osf1_mfs_args osf_ma;
218	struct mfs_args bsd_ma;
219	int error;
220	register_t dummy;
221
222	if ((error = copyin(SCARG(uap, data), &osf_ma, sizeof osf_ma)))
223		return error;
224
225	memset(&bsd_ma, 0, sizeof bsd_ma);
226	bsd_ma.fspec = osf_ma.name;
227	/* XXX export args */
228	bsd_ma.base = osf_ma.base;
229	bsd_ma.size = osf_ma.size;
230
231	return do_sys_mount(l, "mfs", UIO_SYSSPACE, SCARG(uap, path),
232	    SCARG(uap, flags), &bsd_ma, UIO_SYSSPACE, sizeof bsd_ma, &dummy);
233}
234
235static int
236osf1_mount_nfs(struct lwp *l, const struct linux_sys_osf1_mount_args *uap)
237{
238	struct osf1_nfs_args osf_na;
239	struct nfs_args bsd_na;
240	int error;
241	unsigned long leftovers;
242	register_t dummy;
243
244	if ((error = copyin(SCARG(uap, data), &osf_na, sizeof osf_na)))
245		return error;
246
247	memset(&bsd_na, 0, sizeof bsd_na);
248	bsd_na.addr = (struct sockaddr *)osf_na.addr;
249	bsd_na.addrlen = sizeof (struct sockaddr_in);
250	bsd_na.fh = osf_na.fh;
251
252        /* translate flags */
253        bsd_na.flags = emul_flags_translate(osf1_nfs_mount_flags_xtab,
254            osf_na.flags, &leftovers);
255	if (leftovers & OSF1_NFSMNT_HOSTNAME) {
256		leftovers &= ~OSF1_NFSMNT_HOSTNAME;
257		bsd_na.hostname = osf_na.hostname;
258	} else {
259		/* XXX FILL IN HOST NAME WITH IPADDR? */
260	}
261	if (leftovers & OSF1_NFSMNT_TCP) {
262		leftovers &= ~OSF1_NFSMNT_TCP;
263		bsd_na.sotype = SOCK_DGRAM;
264		bsd_na.proto = 0;
265	} else {
266		bsd_na.sotype = SOCK_STREAM;
267		bsd_na.proto = 0;
268	}
269        if (leftovers != 0)
270                return (EINVAL);
271
272	/* copy structure elements based on flags */
273	if (bsd_na.flags & NFSMNT_WSIZE)
274		bsd_na.wsize = osf_na.wsize;
275	if (bsd_na.flags & NFSMNT_RSIZE)
276		bsd_na.rsize = osf_na.rsize;
277	if (bsd_na.flags & NFSMNT_TIMEO)
278		bsd_na.timeo = osf_na.timeo;
279	if (bsd_na.flags & NFSMNT_RETRANS)
280		bsd_na.retrans = osf_na.retrans;
281
282	return do_sys_mount(l, "nfs", UIO_SYSSPACE, SCARG(uap, path),
283	    SCARG(uap, flags), &bsd_na, UIO_SYSSPACE, sizeof bsd_na, &dummy);
284}
285
286int
287linux_sys_osf1_mount(struct lwp *l, const struct linux_sys_osf1_mount_args *uap, register_t *retval)
288{
289
290	if (SCARG(uap, flags) & ~OSF1_MOUNT_FLAGS)
291		return (EINVAL);
292
293	/* XXX - xlate flags */
294
295	switch (SCARG(uap, type)) {
296	case OSF1_MOUNT_NFS:
297		return osf1_mount_nfs(l, uap);
298		break;
299
300	case OSF1_MOUNT_MFS:
301		return osf1_mount_mfs(l, uap);
302
303	default:
304		return (EINVAL);
305	}
306}
307
308int
309linux_sys_osf1_set_program_attributes(struct lwp *l, const struct linux_sys_osf1_set_program_attributes_args *uap, register_t *retval)
310{
311	struct proc *p = l->l_proc;
312	segsz_t tsize, dsize;
313
314	tsize = btoc(SCARG(uap, tsize));
315	dsize = btoc(SCARG(uap, dsize));
316
317	if (dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur)
318		return (ENOMEM);
319#ifdef MAXTSIZ
320	if (tsize > MAXTSIZ)
321		return (ENOMEM);
322#endif
323
324	/* XXXSMP unlocked */
325	p->p_vmspace->vm_taddr = SCARG(uap, taddr);
326	p->p_vmspace->vm_tsize = tsize;
327	p->p_vmspace->vm_daddr = SCARG(uap, daddr);
328	p->p_vmspace->vm_dsize = dsize;
329
330	return (0);
331}
332
333int
334linux_sys_osf1_setitimer(struct lwp *l, const struct linux_sys_osf1_setitimer_args *uap, register_t *retval)
335{
336	struct osf1_itimerval o_itv, o_oitv;
337	struct itimerval b_itv, b_oitv;
338	int which;
339	int error;
340
341	switch (SCARG(uap, which)) {
342	case OSF1_ITIMER_REAL:
343		which = ITIMER_REAL;
344		break;
345
346	case OSF1_ITIMER_VIRTUAL:
347		which = ITIMER_VIRTUAL;
348		break;
349
350	case OSF1_ITIMER_PROF:
351		which = ITIMER_PROF;
352		break;
353
354	default:
355		return (EINVAL);
356	}
357
358	/* get the OSF/1 itimerval argument */
359	error = copyin(SCARG(uap, itv), &o_itv, sizeof o_itv);
360	if (error != 0)
361		return error;
362
363	/* fill in and the NetBSD timeval */
364	memset(&b_itv, 0, sizeof b_itv);
365	b_itv.it_interval.tv_sec = o_itv.it_interval.tv_sec;
366	b_itv.it_interval.tv_usec = o_itv.it_interval.tv_usec;
367	b_itv.it_value.tv_sec = o_itv.it_value.tv_sec;
368	b_itv.it_value.tv_usec = o_itv.it_value.tv_usec;
369
370	if (SCARG(uap, oitv) != NULL) {
371		dogetitimer(l->l_proc, which, &b_oitv);
372		if (error)
373			return error;
374	}
375
376	error = dosetitimer(l->l_proc, which, &b_itv);
377
378	if (error == 0 || SCARG(uap, oitv) == NULL)
379		return error;
380
381	/* fill in and copy out the old timeval */
382	memset(&o_oitv, 0, sizeof o_oitv);
383	o_oitv.it_interval.tv_sec = b_oitv.it_interval.tv_sec;
384	o_oitv.it_interval.tv_usec = b_oitv.it_interval.tv_usec;
385	o_oitv.it_value.tv_sec = b_oitv.it_value.tv_sec;
386	o_oitv.it_value.tv_usec = b_oitv.it_value.tv_usec;
387
388	return copyout(&o_oitv, SCARG(uap, oitv), sizeof o_oitv);
389}
390
391int
392linux_sys_osf1_select(struct lwp *l, const struct linux_sys_osf1_select_args *uap,
393    register_t *retval)
394{
395	struct osf1_timeval otv;
396	struct timespec ats, *ts = NULL;
397	int error;
398
399	if (SCARG(uap, tv)) {
400		/* get the OSF/1 timeval argument */
401		error = copyin(SCARG(uap, tv), &otv, sizeof otv);
402		if (error != 0)
403			return error;
404
405		ats.tv_sec = otv.tv_sec;
406		ats.tv_nsec = otv.tv_usec * 1000;
407		ts = &ats;
408	}
409
410	return selcommon(retval, SCARG(uap, nd), SCARG(uap, in),
411	    SCARG(uap, ou), SCARG(uap, ex), ts, NULL);
412}
413
414int
415linux_sys_osf1_gettimeofday(struct lwp *l, const struct linux_sys_osf1_gettimeofday_args *uap, register_t *retval)
416{
417	struct osf1_timeval otv;
418	struct osf1_timezone otz;
419	struct timeval tv;
420	int error;
421
422	microtime(&tv);
423	memset(&otv, 0, sizeof otv);
424	otv.tv_sec = tv.tv_sec;
425	otv.tv_usec = tv.tv_usec;
426	error = copyout(&otv, SCARG(uap, tv), sizeof otv);
427
428	if (error == 0 && SCARG(uap, tzp) != NULL) {
429		memset(&otz, 0, sizeof otz);
430		error = copyout(&otz, SCARG(uap, tzp), sizeof otz);
431	}
432	return (error);
433}
434
435int
436linux_sys_osf1_getrusage(struct lwp *l, const struct linux_sys_osf1_getrusage_args *uap, register_t *retval)
437{
438	int error, who;
439	struct osf1_rusage osf1_rusage;
440	struct rusage ru;
441	struct proc *p = l->l_proc;
442
443
444	switch (SCARG(uap, who)) {
445	case OSF1_RUSAGE_SELF:
446		who = RUSAGE_SELF;
447		break;
448
449	case OSF1_RUSAGE_CHILDREN:
450		who = RUSAGE_CHILDREN;
451		break;
452
453	case OSF1_RUSAGE_THREAD:		/* XXX not supported */
454	default:
455		return EINVAL;
456	}
457
458	error = getrusage1(p, who, &ru);
459	if (error != 0)
460		return error;
461
462	osf1_cvt_rusage_from_native(&ru, &osf1_rusage);
463
464	return copyout(&osf1_rusage, SCARG(uap, rusage), sizeof osf1_rusage);
465}
466
467int
468linux_sys_osf1_settimeofday(struct lwp *l, const struct linux_sys_osf1_settimeofday_args *uap, register_t *retval)
469{
470	struct osf1_timeval otv;
471	struct timeval tv, *tvp;
472	int error = 0;
473
474	if (SCARG(uap, tv) == NULL)
475		tvp = NULL;
476	else {
477		/* get the OSF/1 timeval argument */
478		error = copyin(SCARG(uap, tv), &otv, sizeof otv);
479		if (error != 0)
480			return error;
481
482		tv.tv_sec = otv.tv_sec;
483		tv.tv_usec = otv.tv_usec;
484		tvp = &tv;
485	}
486
487	/* NetBSD ignores the timezone field */
488
489	return settimeofday1(tvp, false, (const void *)SCARG(uap, tzp), l, true);
490}
491
492int
493linux_sys_osf1_utimes(struct lwp *l, const struct linux_sys_osf1_utimes_args *uap, register_t *retval)
494{
495	struct osf1_timeval otv;
496	struct timeval tv[2], *tvp;
497	int error;
498
499	if (SCARG(uap, tptr) == NULL)
500		tvp = NULL;
501	else {
502		/* get the OSF/1 timeval argument */
503		error = copyin(SCARG(uap, tptr), &otv, sizeof otv);
504		if (error != 0)
505			return error;
506
507		/* fill in and copy out the NetBSD timeval */
508		tv[0].tv_sec = otv.tv_sec;
509		tv[0].tv_usec = otv.tv_usec;
510		/* Set access and modified to the same time */
511		tv[1].tv_sec = otv.tv_sec;
512		tv[1].tv_usec = otv.tv_usec;
513		tvp = tv;
514	}
515
516	return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
517			    tvp, UIO_SYSSPACE);
518}
519
520int
521linux_sys_osf1_statfs(struct lwp *l, const struct linux_sys_osf1_statfs_args *uap, register_t *retval)
522{
523	struct mount *mp;
524	struct statvfs *sp;
525	struct osf1_statfs osfs;
526	int error;
527	struct vnode *vp;
528
529	error = namei_simple_user(SCARG(uap, path),
530				NSM_FOLLOW_TRYEMULROOT, &vp);
531	if (error != 0)
532		return (error);
533	mp = vp->v_mount;
534	sp = &mp->mnt_stat;
535	vrele(vp);
536	if ((error = VFS_STATVFS(mp, sp)))
537		return (error);
538	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
539	osf1_cvt_statfs_from_native(sp, &osfs);
540	return copyout(&osfs, SCARG(uap, buf), uimin(sizeof osfs,
541	    SCARG(uap, len)));
542}
543
544int
545linux_sys_osf1_fstatfs(struct lwp *l, const struct linux_sys_osf1_fstatfs_args *uap, register_t *retval)
546{
547	file_t *fp;
548	struct mount *mp;
549	struct statvfs *sp;
550	struct osf1_statfs osfs;
551	int error;
552
553	/* fd_getvnode() will use the descriptor for us */
554	if ((error = fd_getvnode(SCARG(uap, fd), &fp)))
555		return (error);
556	mp = fp->f_vnode->v_mount;
557	sp = &mp->mnt_stat;
558	if ((error = VFS_STATVFS(mp, sp)))
559		goto out;
560	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
561	osf1_cvt_statfs_from_native(sp, &osfs);
562	error = copyout(&osfs, SCARG(uap, buf), uimin(sizeof osfs,
563	    SCARG(uap, len)));
564 out:
565 	fd_putfile(SCARG(uap, fd));
566	return (error);
567}
568
569int
570linux_sys_osf1_sysinfo(struct lwp *l, const struct linux_sys_osf1_sysinfo_args *uap, register_t *retval)
571{
572	const char *string;
573	size_t slen;
574	int error;
575
576	error = 0;
577	switch (SCARG(uap, cmd)) {
578	case OSF1_SI_SYSNAME:
579		string = ostype;
580		break;
581
582	case OSF1_SI_HOSTNAME:
583		string = hostname;
584		break;
585
586	case OSF1_SI_RELEASE:
587		string = osrelease;
588		break;
589
590	case OSF1_SI_VERSION:
591		goto should_handle;
592
593	case OSF1_SI_MACHINE:
594		string = MACHINE;
595		break;
596
597	case OSF1_SI_ARCHITECTURE:
598		string = MACHINE_ARCH;
599		break;
600
601	case OSF1_SI_HW_SERIAL:
602		string = "666";			/* OSF/1 emulation?  YES! */
603		break;
604
605	case OSF1_SI_HW_PROVIDER:
606		string = "unknown";
607		break;
608
609	case OSF1_SI_SRPC_DOMAIN:
610		goto dont_care;
611
612	case OSF1_SI_SET_HOSTNAME:
613		goto should_handle;
614
615	case OSF1_SI_SET_SYSNAME:
616		goto should_handle;
617
618	case OSF1_SI_SET_SRPC_DOMAIN:
619		goto dont_care;
620
621	default:
622should_handle:
623		printf("osf1_sys_sysinfo(%d, %p, 0x%lx)\n", SCARG(uap, cmd),
624		    SCARG(uap, buf), SCARG(uap,len));
625dont_care:
626		return (EINVAL);
627	};
628
629	slen = strlen(string) + 1;
630	if (SCARG(uap, buf)) {
631		error = copyout(string, SCARG(uap, buf),
632				uimin(slen, SCARG(uap, len)));
633		if (!error && (SCARG(uap, len) > 0) && (SCARG(uap, len) < slen))
634			error = ustore_char(SCARG(uap, buf)
635					    + SCARG(uap, len) - 1, 0);
636	}
637	if (!error)
638		retval[0] = slen;
639
640	return (error);
641}
642
643int
644linux_sys_osf1_usleep_thread(struct lwp *l, const struct linux_sys_osf1_usleep_thread_args *uap, register_t *retval)
645{
646	struct osf1_timeval otv, endotv;
647	struct timeval tv, ntv, endtv;
648	u_long ticks;
649	int error;
650
651	if ((error = copyin(SCARG(uap, sleep), &otv, sizeof otv)))
652		return (error);
653	tv.tv_sec = otv.tv_sec;
654	tv.tv_usec = otv.tv_usec;
655
656	ticks = howmany((u_long)tv.tv_sec * 1000000 + tv.tv_usec, tick);
657	if (ticks == 0)
658		ticks = 1;
659
660	getmicrotime(&tv);
661
662	tsleep(l, PUSER|PCATCH, "uslpthrd", ticks);	/* XXX */
663
664	if (SCARG(uap, slept) != NULL) {
665		getmicrotime(&ntv);
666		timersub(&ntv, &tv, &endtv);
667		if (endtv.tv_sec < 0 || endtv.tv_usec < 0)
668			endtv.tv_sec = endtv.tv_usec = 0;
669
670		memset(&endotv, 0, sizeof(endotv));
671		endotv.tv_sec = endtv.tv_sec;
672		endotv.tv_usec = endtv.tv_usec;
673		error = copyout(&endotv, SCARG(uap, slept), sizeof endotv);
674	}
675	return (error);
676}
677
678int
679linux_sys_osf1_getsysinfo(struct lwp *l, const struct linux_sys_osf1_getsysinfo_args *uap, register_t *retval)
680{
681	extern int ncpus;
682	int error;
683	int unit;
684	long percpu;
685	long proctype;
686	u_int64_t fpflags;
687	struct osf1_cpu_info cpuinfo;
688	const void *data;
689	size_t datalen;
690
691	error = 0;
692
693	switch(SCARG(uap, op))
694	{
695	case OSF_GET_MAX_UPROCS:
696		data = &maxproc;
697		datalen = sizeof(maxproc);
698		break;
699	case OSF_GET_PHYSMEM:
700		data = &physmem;
701		datalen = sizeof(physmem);
702		break;
703	case OSF_GET_MAX_CPU:
704	case OSF_GET_CPUS_IN_BOX:
705		data = &ncpus;
706		datalen = sizeof(ncpus);
707		break;
708	case OSF_GET_IEEE_FP_CONTROL:
709		if (((fpflags = alpha_read_fp_c(l)) & IEEE_INHERIT) != 0) {
710			fpflags |= 1ULL << 63;
711			fpflags &= ~IEEE_INHERIT;
712		}
713		data = &fpflags;
714		datalen = sizeof(fpflags);
715		break;
716	case OSF_GET_CPU_INFO:
717		memset(&cpuinfo, 0, sizeof(cpuinfo));
718#ifdef __alpha__
719		unit = alpha_pal_whami();
720#else
721		unit = 0; /* XXX */
722#endif
723		cpuinfo.current_cpu = unit;
724		cpuinfo.cpus_in_box = ncpus;
725		cpuinfo.cpu_type = LOCATE_PCS(hwrpb, unit)->pcs_proc_type;
726		cpuinfo.ncpus = ncpus;
727		cpuinfo.cpus_present = ncpus;
728		cpuinfo.cpus_running = ncpus;
729		cpuinfo.cpu_binding = 1;
730		cpuinfo.cpu_ex_binding = 0;
731		cpuinfo.mhz = hwrpb->rpb_cc_freq / 1000000;
732		data = &cpuinfo;
733		datalen = sizeof(cpuinfo);
734		break;
735	case OSF_GET_PROC_TYPE:
736#ifdef __alpha__
737		unit = alpha_pal_whami();
738		proctype = LOCATE_PCS(hwrpb, unit)->pcs_proc_type;
739#else
740		proctype = 0;	/* XXX */
741#endif
742		data = &proctype;
743		datalen = sizeof(percpu);
744		break;
745	case OSF_GET_HWRPB: /* note -- osf/1 doesn't have rpb_tbhint[8] */
746		data = hwrpb;
747		datalen = hwrpb->rpb_size;
748		break;
749	case OSF_GET_PLATFORM_NAME:
750		data = platform.model;
751		datalen = strlen(platform.model) + 1;
752		break;
753	default:
754		printf("osf1_getsysinfo called with unknown op=%ld\n",
755		       SCARG(uap, op));
756		/* return EINVAL; */
757		return 0;
758	}
759
760	if (SCARG(uap, nbytes) < datalen)
761		return (EINVAL);
762	error = copyout(data, SCARG(uap, buffer), datalen);
763	if (!error)
764		retval[0] = 1;
765	return (error);
766}
767
768int
769linux_sys_osf1_setsysinfo(struct lwp *l, const struct linux_sys_osf1_setsysinfo_args *uap, register_t *retval)
770{
771	u_int64_t temp;
772	int error;
773
774	error = 0;
775
776	switch(SCARG(uap, op)) {
777	case OSF_SET_IEEE_FP_CONTROL:
778
779		if ((error = copyin(SCARG(uap, buffer), &temp, sizeof(temp))))
780			break;
781		if (temp >> 63 != 0)
782			temp |= IEEE_INHERIT;
783		alpha_write_fp_c(l, temp);
784		break;
785	default:
786		uprintf("osf1_setsysinfo called with op=%ld\n", SCARG(uap, op));
787		//error = EINVAL;
788	}
789	retval[0] = 0;
790	return (error);
791}
792