xref: /netbsd-src/sys/compat/linux/common/linux_misc.c (revision 17dd36da8292193180754d5047c0926dbb56818c)
1 /*	$NetBSD: linux_misc.c,v 1.87 2001/03/30 17:16:34 jdolecek Exp $	*/
2 
3 /*-
4  * Copyright (c) 1995, 1998, 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Frank van der Linden and Eric Haszlakiewicz; by Jason R. Thorpe
9  * of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Linux compatibility module. Try to deal with various Linux system calls.
42  */
43 
44 /*
45  * These functions have been moved to multiarch to allow
46  * selection of which machines include them to be
47  * determined by the individual files.linux_<arch> files.
48  *
49  * Function in multiarch:
50  *	linux_sys_break			: linux_break.c
51  *	linux_sys_alarm			: linux_misc_notalpha.c
52  *	linux_sys_getresgid		: linux_misc_notalpha.c
53  *	linux_sys_nice			: linux_misc_notalpha.c
54  *	linux_sys_readdir		: linux_misc_notalpha.c
55  *	linux_sys_setresgid		: linux_misc_notalpha.c
56  *	linux_sys_time			: linux_misc_notalpha.c
57  *	linux_sys_utime			: linux_misc_notalpha.c
58  *	linux_sys_waitpid		: linux_misc_notalpha.c
59  *	linux_sys_old_mmap		: linux_oldmmap.c
60  *	linux_sys_oldolduname		: linux_oldolduname.c
61  *	linux_sys_oldselect		: linux_oldselect.c
62  *	linux_sys_olduname		: linux_olduname.c
63  *	linux_sys_pipe			: linux_pipe.c
64  */
65 
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/namei.h>
69 #include <sys/proc.h>
70 #include <sys/dirent.h>
71 #include <sys/file.h>
72 #include <sys/stat.h>
73 #include <sys/filedesc.h>
74 #include <sys/ioctl.h>
75 #include <sys/kernel.h>
76 #include <sys/malloc.h>
77 #include <sys/mbuf.h>
78 #include <sys/mman.h>
79 #include <sys/mount.h>
80 #include <sys/reboot.h>
81 #include <sys/resource.h>
82 #include <sys/resourcevar.h>
83 #include <sys/signal.h>
84 #include <sys/signalvar.h>
85 #include <sys/socket.h>
86 #include <sys/time.h>
87 #include <sys/times.h>
88 #include <sys/vnode.h>
89 #include <sys/uio.h>
90 #include <sys/wait.h>
91 #include <sys/utsname.h>
92 #include <sys/unistd.h>
93 #include <sys/swap.h>		/* for SWAP_ON */
94 #include <sys/sysctl.h>		/* for KERN_DOMAINNAME */
95 
96 #include <sys/ptrace.h>
97 #include <machine/ptrace.h>
98 
99 #include <sys/syscallargs.h>
100 
101 #include <compat/linux/common/linux_types.h>
102 #include <compat/linux/common/linux_signal.h>
103 
104 #include <compat/linux/linux_syscallargs.h>
105 
106 #include <compat/linux/common/linux_fcntl.h>
107 #include <compat/linux/common/linux_mmap.h>
108 #include <compat/linux/common/linux_dirent.h>
109 #include <compat/linux/common/linux_util.h>
110 #include <compat/linux/common/linux_misc.h>
111 #include <compat/linux/common/linux_ptrace.h>
112 #include <compat/linux/common/linux_reboot.h>
113 #include <compat/linux/common/linux_emuldata.h>
114 
115 const int linux_ptrace_request_map[] = {
116 	LINUX_PTRACE_TRACEME,	PT_TRACE_ME,
117 	LINUX_PTRACE_PEEKTEXT,	PT_READ_I,
118 	LINUX_PTRACE_PEEKDATA,	PT_READ_D,
119 	LINUX_PTRACE_POKETEXT,	PT_WRITE_I,
120 	LINUX_PTRACE_POKEDATA,	PT_WRITE_D,
121 	LINUX_PTRACE_CONT,	PT_CONTINUE,
122 	LINUX_PTRACE_KILL,	PT_KILL,
123 	LINUX_PTRACE_ATTACH,	PT_ATTACH,
124 	LINUX_PTRACE_DETACH,	PT_DETACH,
125 #ifdef PT_STEP
126 	LINUX_PTRACE_SINGLESTEP,	PT_STEP,
127 #endif
128 	-1
129 };
130 
131 /* Local linux_misc.c functions: */
132 static void bsd_to_linux_statfs __P((struct statfs *, struct linux_statfs *));
133 
134 /*
135  * The information on a terminated (or stopped) process needs
136  * to be converted in order for Linux binaries to get a valid signal
137  * number out of it.
138  */
139 void
140 bsd_to_linux_wstat(st)
141 	int *st;
142 {
143 
144 	int sig;
145 
146 	if (WIFSIGNALED(*st)) {
147 		sig = WTERMSIG(*st);
148 		if (sig >= 0 && sig < NSIG)
149 			*st= (*st& ~0177) | native_to_linux_sig[sig];
150 	} else if (WIFSTOPPED(*st)) {
151 		sig = WSTOPSIG(*st);
152 		if (sig >= 0 && sig < NSIG)
153 			*st = (*st & ~0xff00) | (native_to_linux_sig[sig] << 8);
154 	}
155 }
156 
157 /*
158  * This is very much the same as waitpid()
159  */
160 int
161 linux_sys_wait4(p, v, retval)
162 	struct proc *p;
163 	void *v;
164 	register_t *retval;
165 {
166 	struct linux_sys_wait4_args /* {
167 		syscallarg(int) pid;
168 		syscallarg(int *) status;
169 		syscallarg(int) options;
170 		syscallarg(struct rusage *) rusage;
171 	} */ *uap = v;
172 	struct sys_wait4_args w4a;
173 	int error, *status, tstat, options, linux_options;
174 	caddr_t sg;
175 
176 	if (SCARG(uap, status) != NULL) {
177 		sg = stackgap_init(p->p_emul);
178 		status = (int *) stackgap_alloc(&sg, sizeof *status);
179 	} else
180 		status = NULL;
181 
182 	linux_options = SCARG(uap, options);
183 	options = 0;
184 	if (linux_options &
185 	    ~(LINUX_WAIT4_WNOHANG|LINUX_WAIT4_WUNTRACED|LINUX_WAIT4_WCLONE))
186 		return (EINVAL);
187 
188 	if (linux_options & LINUX_WAIT4_WNOHANG)
189 		options |= WNOHANG;
190 	if (linux_options & LINUX_WAIT4_WUNTRACED)
191 		options |= WUNTRACED;
192 	if (linux_options & LINUX_WAIT4_WCLONE)
193 		options |= WALTSIG;
194 
195 	SCARG(&w4a, pid) = SCARG(uap, pid);
196 	SCARG(&w4a, status) = status;
197 	SCARG(&w4a, options) = options;
198 	SCARG(&w4a, rusage) = SCARG(uap, rusage);
199 
200 	if ((error = sys_wait4(p, &w4a, retval)))
201 		return error;
202 
203 	sigdelset(&p->p_sigctx.ps_siglist, SIGCHLD);
204 
205 	if (status != NULL) {
206 		if ((error = copyin(status, &tstat, sizeof tstat)))
207 			return error;
208 
209 		bsd_to_linux_wstat(&tstat);
210 		return copyout(&tstat, SCARG(uap, status), sizeof tstat);
211 	}
212 
213 	return 0;
214 }
215 
216 /*
217  * Linux brk(2). The check if the new address is >= the old one is
218  * done in the kernel in Linux. NetBSD does it in the library.
219  */
220 int
221 linux_sys_brk(p, v, retval)
222 	struct proc *p;
223 	void *v;
224 	register_t *retval;
225 {
226 	struct linux_sys_brk_args /* {
227 		syscallarg(char *) nsize;
228 	} */ *uap = v;
229 	char *nbrk = SCARG(uap, nsize);
230 	struct sys_obreak_args oba;
231 	struct vmspace *vm = p->p_vmspace;
232 	struct linux_emuldata *ed = (struct linux_emuldata*)p->p_emuldata;
233 
234 	SCARG(&oba, nsize) = nbrk;
235 
236 	if ((caddr_t) nbrk > vm->vm_daddr && sys_obreak(p, &oba, retval) == 0)
237 		ed->p_break = (char*)nbrk;
238 	else
239 		nbrk = ed->p_break;
240 
241 	retval[0] = (register_t)nbrk;
242 
243 	return 0;
244 }
245 
246 /*
247  * Convert BSD statfs structure to Linux statfs structure.
248  * The Linux structure has less fields, and it also wants
249  * the length of a name in a dir entry in a field, which
250  * we fake (probably the wrong way).
251  */
252 static void
253 bsd_to_linux_statfs(bsp, lsp)
254 	struct statfs *bsp;
255 	struct linux_statfs *lsp;
256 {
257 
258 	lsp->l_ftype = bsp->f_type;
259 	lsp->l_fbsize = bsp->f_bsize;
260 	lsp->l_fblocks = bsp->f_blocks;
261 	lsp->l_fbfree = bsp->f_bfree;
262 	lsp->l_fbavail = bsp->f_bavail;
263 	lsp->l_ffiles = bsp->f_files;
264 	lsp->l_fffree = bsp->f_ffree;
265 	lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
266 	lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
267 	lsp->l_fnamelen = MAXNAMLEN;	/* XXX */
268 }
269 
270 /*
271  * Implement the fs stat functions. Straightforward.
272  */
273 int
274 linux_sys_statfs(p, v, retval)
275 	struct proc *p;
276 	void *v;
277 	register_t *retval;
278 {
279 	struct linux_sys_statfs_args /* {
280 		syscallarg(const char *) path;
281 		syscallarg(struct linux_statfs *) sp;
282 	} */ *uap = v;
283 	struct statfs btmp, *bsp;
284 	struct linux_statfs ltmp;
285 	struct sys_statfs_args bsa;
286 	caddr_t sg;
287 	int error;
288 
289 	sg = stackgap_init(p->p_emul);
290 	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
291 
292 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
293 
294 	SCARG(&bsa, path) = SCARG(uap, path);
295 	SCARG(&bsa, buf) = bsp;
296 
297 	if ((error = sys_statfs(p, &bsa, retval)))
298 		return error;
299 
300 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
301 		return error;
302 
303 	bsd_to_linux_statfs(&btmp, &ltmp);
304 
305 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
306 }
307 
308 int
309 linux_sys_fstatfs(p, v, retval)
310 	struct proc *p;
311 	void *v;
312 	register_t *retval;
313 {
314 	struct linux_sys_fstatfs_args /* {
315 		syscallarg(int) fd;
316 		syscallarg(struct linux_statfs *) sp;
317 	} */ *uap = v;
318 	struct statfs btmp, *bsp;
319 	struct linux_statfs ltmp;
320 	struct sys_fstatfs_args bsa;
321 	caddr_t sg;
322 	int error;
323 
324 	sg = stackgap_init(p->p_emul);
325 	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
326 
327 	SCARG(&bsa, fd) = SCARG(uap, fd);
328 	SCARG(&bsa, buf) = bsp;
329 
330 	if ((error = sys_fstatfs(p, &bsa, retval)))
331 		return error;
332 
333 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
334 		return error;
335 
336 	bsd_to_linux_statfs(&btmp, &ltmp);
337 
338 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
339 }
340 
341 char linux_sysname[] = "Linux";
342 char linux_release[] = "2.0.38";
343 char linux_version[] = "#0 Sun Apr 1 11:11:11 MET 2000";
344 
345 /*
346  * uname(). Just copy the info from the various strings stored in the
347  * kernel, and put it in the Linux utsname structure. That structure
348  * is almost the same as the NetBSD one, only it has fields 65 characters
349  * long, and an extra domainname field.
350  */
351 int
352 linux_sys_uname(p, v, retval)
353 	struct proc *p;
354 	void *v;
355 	register_t *retval;
356 {
357 	struct linux_sys_uname_args /* {
358 		syscallarg(struct linux_utsname *) up;
359 	} */ *uap = v;
360 	struct linux_utsname luts;
361 
362 	strncpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
363 	strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
364 	strncpy(luts.l_release, linux_release, sizeof(luts.l_release));
365 	strncpy(luts.l_version, linux_version, sizeof(luts.l_version));
366 	strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
367 	strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
368 
369 	return copyout(&luts, SCARG(uap, up), sizeof(luts));
370 }
371 
372 /* Used directly on: alpha, mips, ppc, sparc, sparc64 */
373 /* Used indirectly on: arm, i386, m68k */
374 
375 /*
376  * New type Linux mmap call.
377  * Only called directly on machines with >= 6 free regs.
378  */
379 int
380 linux_sys_mmap(p, v, retval)
381 	struct proc *p;
382 	void *v;
383 	register_t *retval;
384 {
385 	struct linux_sys_mmap_args /* {
386 		syscallarg(unsigned long) addr;
387 		syscallarg(size_t) len;
388 		syscallarg(int) prot;
389 		syscallarg(int) flags;
390 		syscallarg(int) fd;
391 		syscallarg(off_t) offset;
392 	} */ *uap = v;
393 	struct sys_mmap_args cma;
394 	int flags;
395 
396 	flags = 0;
397 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_SHARED, MAP_SHARED);
398 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_PRIVATE, MAP_PRIVATE);
399 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_FIXED, MAP_FIXED);
400 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_ANON, MAP_ANON);
401 	/* XXX XAX ERH: Any other flags here?  There are more defined... */
402 
403 	SCARG(&cma,addr) = (void *)SCARG(uap, addr);
404 	SCARG(&cma,len) = SCARG(uap, len);
405 	SCARG(&cma,prot) = SCARG(uap, prot);
406 	if (SCARG(&cma,prot) & VM_PROT_WRITE) /* XXX */
407 		SCARG(&cma,prot) |= VM_PROT_READ;
408 	SCARG(&cma,flags) = flags;
409 	SCARG(&cma,fd) = flags & MAP_ANON ? -1 : SCARG(uap, fd);
410 	SCARG(&cma,pad) = 0;
411 	SCARG(&cma,pos) = SCARG(uap, offset);
412 
413 	return sys_mmap(p, &cma, retval);
414 }
415 
416 int
417 linux_sys_mremap(p, v, retval)
418 	struct proc *p;
419 	void *v;
420 	register_t *retval;
421 {
422 	struct linux_sys_mremap_args /* {
423 		syscallarg(void *) old_address;
424 		syscallarg(size_t) old_size;
425 		syscallarg(size_t) new_size;
426 		syscallarg(u_long) flags;
427 	} */ *uap = v;
428 	struct sys_munmap_args mua;
429 	size_t old_size, new_size;
430 	int error;
431 
432 	old_size = round_page(SCARG(uap, old_size));
433 	new_size = round_page(SCARG(uap, new_size));
434 
435 	/*
436 	 * Growing mapped region.
437 	 */
438 	if (new_size > old_size) {
439 		/*
440 		 * XXX Implement me.  What we probably want to do is
441 		 * XXX dig out the guts of the old mapping, mmap that
442 		 * XXX object again with the new size, then munmap
443 		 * XXX the old mapping.
444 		 */
445 		*retval = 0;
446 		return (ENOMEM);
447 	}
448 
449 	/*
450 	 * Shrinking mapped region.
451 	 */
452 	if (new_size < old_size) {
453 		SCARG(&mua, addr) = (caddr_t)SCARG(uap, old_address) +
454 		    new_size;
455 		SCARG(&mua, len) = old_size - new_size;
456 		error = sys_munmap(p, &mua, retval);
457 		*retval = error ? 0 : (register_t)SCARG(uap, old_address);
458 		return (error);
459 	}
460 
461 	/*
462 	 * No change.
463 	 */
464 	*retval = (register_t)SCARG(uap, old_address);
465 	return (0);
466 }
467 
468 int
469 linux_sys_msync(p, v, retval)
470 	struct proc *p;
471 	void *v;
472 	register_t *retval;
473 {
474 	struct linux_sys_msync_args /* {
475 		syscallarg(caddr_t) addr;
476 		syscallarg(int) len;
477 		syscallarg(int) fl;
478 	} */ *uap = v;
479 
480 	struct sys___msync13_args bma;
481 
482 	/* flags are ignored */
483 	SCARG(&bma, addr) = SCARG(uap, addr);
484 	SCARG(&bma, len) = SCARG(uap, len);
485 	SCARG(&bma, flags) = SCARG(uap, fl);
486 
487 	return sys___msync13(p, &bma, retval);
488 }
489 
490 /*
491  * This code is partly stolen from src/lib/libc/compat-43/times.c
492  * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
493  */
494 
495 #define CLK_TCK 100
496 #define	CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
497 
498 int
499 linux_sys_times(p, v, retval)
500 	struct proc *p;
501 	void *v;
502 	register_t *retval;
503 {
504 	struct linux_sys_times_args /* {
505 		syscallarg(struct times *) tms;
506 	} */ *uap = v;
507 	struct timeval t;
508 	struct linux_tms ltms;
509 	struct rusage ru;
510 	int error, s;
511 
512 	calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
513 	ltms.ltms_utime = CONVTCK(ru.ru_utime);
514 	ltms.ltms_stime = CONVTCK(ru.ru_stime);
515 
516 	ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
517 	ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
518 
519 	if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
520 		return error;
521 
522 	s = splclock();
523 	timersub(&time, &boottime, &t);
524 	splx(s);
525 
526 	retval[0] = ((linux_clock_t)(CONVTCK(t)));
527 	return 0;
528 }
529 
530 /*
531  * Linux 'readdir' call. This code is mostly taken from the
532  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
533  * an attempt has been made to keep it a little cleaner (failing
534  * miserably, because of the cruft needed if count 1 is passed).
535  *
536  * The d_off field should contain the offset of the next valid entry,
537  * but in Linux it has the offset of the entry itself. We emulate
538  * that bug here.
539  *
540  * Read in BSD-style entries, convert them, and copy them out.
541  *
542  * Note that this doesn't handle union-mounted filesystems.
543  */
544 int
545 linux_sys_getdents(p, v, retval)
546 	struct proc *p;
547 	void *v;
548 	register_t *retval;
549 {
550 	struct linux_sys_getdents_args /* {
551 		syscallarg(int) fd;
552 		syscallarg(struct linux_dirent *) dent;
553 		syscallarg(unsigned int) count;
554 	} */ *uap = v;
555 	struct dirent *bdp;
556 	struct vnode *vp;
557 	caddr_t	inp, buf;		/* BSD-format */
558 	int len, reclen;		/* BSD-format */
559 	caddr_t outp;			/* Linux-format */
560 	int resid, linux_reclen = 0;	/* Linux-format */
561 	struct file *fp;
562 	struct uio auio;
563 	struct iovec aiov;
564 	struct linux_dirent idb;
565 	off_t off;		/* true file offset */
566 	int buflen, error, eofflag, nbytes, oldcall;
567 	struct vattr va;
568 	off_t *cookiebuf = NULL, *cookie;
569 	int ncookies;
570 
571 	/* getvnode() will use the descriptor for us */
572 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
573 		return (error);
574 
575 	if ((fp->f_flag & FREAD) == 0) {
576 		error = EBADF;
577 		goto out1;
578 	}
579 
580 	vp = (struct vnode *)fp->f_data;
581 	if (vp->v_type != VDIR) {
582 		error = EINVAL;
583 		goto out1;
584 	}
585 
586 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
587 		goto out1;
588 
589 	nbytes = SCARG(uap, count);
590 	if (nbytes == 1) {	/* emulating old, broken behaviour */
591 		nbytes = sizeof (struct linux_dirent);
592 		buflen = max(va.va_blocksize, nbytes);
593 		oldcall = 1;
594 	} else {
595 		buflen = min(MAXBSIZE, nbytes);
596 		if (buflen < va.va_blocksize)
597 			buflen = va.va_blocksize;
598 		oldcall = 0;
599 	}
600 	buf = malloc(buflen, M_TEMP, M_WAITOK);
601 
602 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
603 	off = fp->f_offset;
604 again:
605 	aiov.iov_base = buf;
606 	aiov.iov_len = buflen;
607 	auio.uio_iov = &aiov;
608 	auio.uio_iovcnt = 1;
609 	auio.uio_rw = UIO_READ;
610 	auio.uio_segflg = UIO_SYSSPACE;
611 	auio.uio_procp = p;
612 	auio.uio_resid = buflen;
613 	auio.uio_offset = off;
614 	/*
615          * First we read into the malloc'ed buffer, then
616          * we massage it into user space, one record at a time.
617          */
618 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
619 	    &ncookies);
620 	if (error)
621 		goto out;
622 
623 	inp = buf;
624 	outp = (caddr_t)SCARG(uap, dent);
625 	resid = nbytes;
626 	if ((len = buflen - auio.uio_resid) == 0)
627 		goto eof;
628 
629 	for (cookie = cookiebuf; len > 0; len -= reclen) {
630 		bdp = (struct dirent *)inp;
631 		reclen = bdp->d_reclen;
632 		if (reclen & 3)
633 			panic("linux_readdir");
634 		if (bdp->d_fileno == 0) {
635 			inp += reclen;	/* it is a hole; squish it out */
636 			off = *cookie++;
637 			continue;
638 		}
639 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
640 		if (reclen > len || resid < linux_reclen) {
641 			/* entry too big for buffer, so just stop */
642 			outp++;
643 			break;
644 		}
645 		/*
646 		 * Massage in place to make a Linux-shaped dirent (otherwise
647 		 * we have to worry about touching user memory outside of
648 		 * the copyout() call).
649 		 */
650 		idb.d_ino = (linux_ino_t)bdp->d_fileno;
651 		/*
652 		 * The old readdir() call misuses the offset and reclen fields.
653 		 */
654 		if (oldcall) {
655 			idb.d_off = (linux_off_t)linux_reclen;
656 			idb.d_reclen = (u_short)bdp->d_namlen;
657 		} else {
658 			if (sizeof (linux_off_t) < 4 && (off >> 32) != 0) {
659 				compat_offseterr(vp, "linux_getdents");
660 				error = EINVAL;
661 				goto out;
662 			}
663 			idb.d_off = (linux_off_t)off;
664 			idb.d_reclen = (u_short)linux_reclen;
665 		}
666 		strcpy(idb.d_name, bdp->d_name);
667 		if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
668 			goto out;
669 		/* advance past this real entry */
670 		inp += reclen;
671 		off = *cookie++;	/* each entry points to itself */
672 		/* advance output past Linux-shaped entry */
673 		outp += linux_reclen;
674 		resid -= linux_reclen;
675 		if (oldcall)
676 			break;
677 	}
678 
679 	/* if we squished out the whole block, try again */
680 	if (outp == (caddr_t)SCARG(uap, dent))
681 		goto again;
682 	fp->f_offset = off;	/* update the vnode offset */
683 
684 	if (oldcall)
685 		nbytes = resid + linux_reclen;
686 
687 eof:
688 	*retval = nbytes - resid;
689 out:
690 	VOP_UNLOCK(vp, 0);
691 	if (cookiebuf)
692 		free(cookiebuf, M_TEMP);
693 	free(buf, M_TEMP);
694  out1:
695 	FILE_UNUSE(fp, p);
696 	return error;
697 }
698 
699 /*
700  * Even when just using registers to pass arguments to syscalls you can
701  * have 5 of them on the i386. So this newer version of select() does
702  * this.
703  */
704 int
705 linux_sys_select(p, v, retval)
706 	struct proc *p;
707 	void *v;
708 	register_t *retval;
709 {
710 	struct linux_sys_select_args /* {
711 		syscallarg(int) nfds;
712 		syscallarg(fd_set *) readfds;
713 		syscallarg(fd_set *) writefds;
714 		syscallarg(fd_set *) exceptfds;
715 		syscallarg(struct timeval *) timeout;
716 	} */ *uap = v;
717 
718 	return linux_select1(p, retval, SCARG(uap, nfds), SCARG(uap, readfds),
719 	    SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
720 }
721 
722 /*
723  * Common code for the old and new versions of select(). A couple of
724  * things are important:
725  * 1) return the amount of time left in the 'timeout' parameter
726  * 2) select never returns ERESTART on Linux, always return EINTR
727  */
728 int
729 linux_select1(p, retval, nfds, readfds, writefds, exceptfds, timeout)
730 	struct proc *p;
731 	register_t *retval;
732 	int nfds;
733 	fd_set *readfds, *writefds, *exceptfds;
734 	struct timeval *timeout;
735 {
736 	struct sys_select_args bsa;
737 	struct timeval tv0, tv1, utv, *tvp;
738 	caddr_t sg;
739 	int error;
740 
741 	SCARG(&bsa, nd) = nfds;
742 	SCARG(&bsa, in) = readfds;
743 	SCARG(&bsa, ou) = writefds;
744 	SCARG(&bsa, ex) = exceptfds;
745 	SCARG(&bsa, tv) = timeout;
746 
747 	/*
748 	 * Store current time for computation of the amount of
749 	 * time left.
750 	 */
751 	if (timeout) {
752 		if ((error = copyin(timeout, &utv, sizeof(utv))))
753 			return error;
754 		if (itimerfix(&utv)) {
755 			/*
756 			 * The timeval was invalid.  Convert it to something
757 			 * valid that will act as it does under Linux.
758 			 */
759 			sg = stackgap_init(p->p_emul);
760 			tvp = stackgap_alloc(&sg, sizeof(utv));
761 			utv.tv_sec += utv.tv_usec / 1000000;
762 			utv.tv_usec %= 1000000;
763 			if (utv.tv_usec < 0) {
764 				utv.tv_sec -= 1;
765 				utv.tv_usec += 1000000;
766 			}
767 			if (utv.tv_sec < 0)
768 				timerclear(&utv);
769 			if ((error = copyout(&utv, tvp, sizeof(utv))))
770 				return error;
771 			SCARG(&bsa, tv) = tvp;
772 		}
773 		microtime(&tv0);
774 	}
775 
776 	error = sys_select(p, &bsa, retval);
777 	if (error) {
778 		/*
779 		 * See fs/select.c in the Linux kernel.  Without this,
780 		 * Maelstrom doesn't work.
781 		 */
782 		if (error == ERESTART)
783 			error = EINTR;
784 		return error;
785 	}
786 
787 	if (timeout) {
788 		if (*retval) {
789 			/*
790 			 * Compute how much time was left of the timeout,
791 			 * by subtracting the current time and the time
792 			 * before we started the call, and subtracting
793 			 * that result from the user-supplied value.
794 			 */
795 			microtime(&tv1);
796 			timersub(&tv1, &tv0, &tv1);
797 			timersub(&utv, &tv1, &utv);
798 			if (utv.tv_sec < 0)
799 				timerclear(&utv);
800 		} else
801 			timerclear(&utv);
802 		if ((error = copyout(&utv, timeout, sizeof(utv))))
803 			return error;
804 	}
805 
806 	return 0;
807 }
808 
809 /*
810  * Get the process group of a certain process. Look it up
811  * and return the value.
812  */
813 int
814 linux_sys_getpgid(p, v, retval)
815 	struct proc *p;
816 	void *v;
817 	register_t *retval;
818 {
819 	struct linux_sys_getpgid_args /* {
820 		syscallarg(int) pid;
821 	} */ *uap = v;
822 	struct proc *targp;
823 
824 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
825 		if ((targp = pfind(SCARG(uap, pid))) == 0)
826 			return ESRCH;
827 	}
828 	else
829 		targp = p;
830 
831 	retval[0] = targp->p_pgid;
832 	return 0;
833 }
834 
835 /*
836  * Set the 'personality' (emulation mode) for the current process. Only
837  * accept the Linux personality here (0). This call is needed because
838  * the Linux ELF crt0 issues it in an ugly kludge to make sure that
839  * ELF binaries run in Linux mode, not SVR4 mode.
840  */
841 int
842 linux_sys_personality(p, v, retval)
843 	struct proc *p;
844 	void *v;
845 	register_t *retval;
846 {
847 	struct linux_sys_personality_args /* {
848 		syscallarg(int) per;
849 	} */ *uap = v;
850 
851 	if (SCARG(uap, per) != 0)
852 		return EINVAL;
853 	retval[0] = 0;
854 	return 0;
855 }
856 
857 #if defined(__i386__) || defined(__m68k__)
858 /*
859  * The calls are here because of type conversions.
860  */
861 int
862 linux_sys_setreuid16(p, v, retval)
863 	struct proc *p;
864 	void *v;
865 	register_t *retval;
866 {
867 	struct linux_sys_setreuid16_args /* {
868 		syscallarg(int) ruid;
869 		syscallarg(int) euid;
870 	} */ *uap = v;
871 	struct sys_setreuid_args bsa;
872 
873 	SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
874 		(uid_t)-1 : SCARG(uap, ruid);
875 	SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
876 		(uid_t)-1 : SCARG(uap, euid);
877 
878 	return sys_setreuid(p, &bsa, retval);
879 }
880 
881 int
882 linux_sys_setregid16(p, v, retval)
883 	struct proc *p;
884 	void *v;
885 	register_t *retval;
886 {
887 	struct linux_sys_setregid16_args /* {
888 		syscallarg(int) rgid;
889 		syscallarg(int) egid;
890 	} */ *uap = v;
891 	struct sys_setregid_args bsa;
892 
893 	SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
894 		(uid_t)-1 : SCARG(uap, rgid);
895 	SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
896 		(uid_t)-1 : SCARG(uap, egid);
897 
898 	return sys_setregid(p, &bsa, retval);
899 }
900 
901 int
902 linux_sys_setresuid16(p, v, retval)
903 	struct proc *p;
904 	void *v;
905 	register_t *retval;
906 {
907 	struct linux_sys_setresuid16_args /* {
908 		syscallarg(uid_t) ruid;
909 		syscallarg(uid_t) euid;
910 		syscallarg(uid_t) suid;
911 	} */ *uap = v;
912 	struct linux_sys_setresuid16_args lsa;
913 
914 	SCARG(&lsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
915 		(uid_t)-1 : SCARG(uap, ruid);
916 	SCARG(&lsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
917 		(uid_t)-1 : SCARG(uap, euid);
918 	SCARG(&lsa, suid) = ((linux_uid_t)SCARG(uap, suid) == (linux_uid_t)-1) ?
919 		(uid_t)-1 : SCARG(uap, suid);
920 
921 	return linux_sys_setresuid(p, &lsa, retval);
922 }
923 
924 int
925 linux_sys_setresgid16(p, v, retval)
926 	struct proc *p;
927 	void *v;
928 	register_t *retval;
929 {
930 	struct linux_sys_setresgid16_args /* {
931 		syscallarg(gid_t) rgid;
932 		syscallarg(gid_t) egid;
933 		syscallarg(gid_t) sgid;
934 	} */ *uap = v;
935 	struct linux_sys_setresgid16_args lsa;
936 
937 	SCARG(&lsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
938 		(gid_t)-1 : SCARG(uap, rgid);
939 	SCARG(&lsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
940 		(gid_t)-1 : SCARG(uap, egid);
941 	SCARG(&lsa, sgid) = ((linux_gid_t)SCARG(uap, sgid) == (linux_gid_t)-1) ?
942 		(gid_t)-1 : SCARG(uap, sgid);
943 
944 	return linux_sys_setresgid(p, &lsa, retval);
945 }
946 
947 int
948 linux_sys_getgroups16(p, v, retval)
949 	struct proc *p;
950 	void *v;
951 	register_t *retval;
952 {
953 	struct linux_sys_getgroups16_args /* {
954 		syscallarg(int) gidsetsize;
955 		syscallarg(linux_gid_t *) gidset;
956 	} */ *uap = v;
957 	caddr_t sg;
958 	int n, error, i;
959 	struct sys_getgroups_args bsa;
960 	gid_t *bset, *kbset;
961 	linux_gid_t *lset;
962 	struct pcred *pc = p->p_cred;
963 
964 	n = SCARG(uap, gidsetsize);
965 	if (n < 0)
966 		return EINVAL;
967 	error = 0;
968 	bset = kbset = NULL;
969 	lset = NULL;
970 	if (n > 0) {
971 		n = min(pc->pc_ucred->cr_ngroups, n);
972 		sg = stackgap_init(p->p_emul);
973 		bset = stackgap_alloc(&sg, n * sizeof (gid_t));
974 		kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
975 		lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
976 		if (bset == NULL || kbset == NULL || lset == NULL)
977 			return ENOMEM;
978 		SCARG(&bsa, gidsetsize) = n;
979 		SCARG(&bsa, gidset) = bset;
980 		error = sys_getgroups(p, &bsa, retval);
981 		if (error != 0)
982 			goto out;
983 		error = copyin(bset, kbset, n * sizeof (gid_t));
984 		if (error != 0)
985 			goto out;
986 		for (i = 0; i < n; i++)
987 			lset[i] = (linux_gid_t)kbset[i];
988 		error = copyout(lset, SCARG(uap, gidset),
989 		    n * sizeof (linux_gid_t));
990 	} else
991 		*retval = pc->pc_ucred->cr_ngroups;
992 out:
993 	if (kbset != NULL)
994 		free(kbset, M_TEMP);
995 	if (lset != NULL)
996 		free(lset, M_TEMP);
997 	return error;
998 }
999 
1000 int
1001 linux_sys_setgroups16(p, v, retval)
1002 	struct proc *p;
1003 	void *v;
1004 	register_t *retval;
1005 {
1006 	struct linux_sys_setgroups16_args /* {
1007 		syscallarg(int) gidsetsize;
1008 		syscallarg(linux_gid_t *) gidset;
1009 	} */ *uap = v;
1010 	caddr_t sg;
1011 	int n;
1012 	int error, i;
1013 	struct sys_setgroups_args bsa;
1014 	gid_t *bset, *kbset;
1015 	linux_gid_t *lset;
1016 
1017 	n = SCARG(uap, gidsetsize);
1018 	if (n < 0 || n > NGROUPS)
1019 		return EINVAL;
1020 	sg = stackgap_init(p->p_emul);
1021 	bset = stackgap_alloc(&sg, n * sizeof (gid_t));
1022 	lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
1023 	kbset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
1024 	if (lset == NULL || bset == NULL)
1025 		return ENOMEM;
1026 	error = copyin(SCARG(uap, gidset), lset, n * sizeof (linux_gid_t));
1027 	if (error != 0)
1028 		goto out;
1029 	for (i = 0; i < n; i++)
1030 		kbset[i] = (gid_t)lset[i];
1031 	error = copyout(kbset, bset, n * sizeof (gid_t));
1032 	if (error != 0)
1033 		goto out;
1034 	SCARG(&bsa, gidsetsize) = n;
1035 	SCARG(&bsa, gidset) = bset;
1036 	error = sys_setgroups(p, &bsa, retval);
1037 
1038 out:
1039 	if (lset != NULL)
1040 		free(lset, M_TEMP);
1041 	if (kbset != NULL)
1042 		free(kbset, M_TEMP);
1043 
1044 	return error;
1045 }
1046 
1047 #endif /* __i386__ || __m68k__ */
1048 
1049 /*
1050  * We have nonexistent fsuid equal to uid.
1051  * If modification is requested, refuse.
1052  */
1053 int
1054 linux_sys_setfsuid(p, v, retval)
1055 	 struct proc *p;
1056 	 void *v;
1057 	 register_t *retval;
1058 {
1059 	 struct linux_sys_setfsuid_args /* {
1060 		 syscallarg(uid_t) uid;
1061 	 } */ *uap = v;
1062 	 uid_t uid;
1063 
1064 	 uid = SCARG(uap, uid);
1065 	 if (p->p_cred->p_ruid != uid)
1066 		 return sys_nosys(p, v, retval);
1067 	 else
1068 		 return (0);
1069 }
1070 
1071 /* XXX XXX XXX */
1072 #ifndef alpha
1073 int
1074 linux_sys_getfsuid(p, v, retval)
1075 	struct proc *p;
1076 	void *v;
1077 	register_t *retval;
1078 {
1079 	return sys_getuid(p, v, retval);
1080 }
1081 #endif
1082 
1083 int
1084 linux_sys___sysctl(p, v, retval)
1085 	struct proc *p;
1086 	void *v;
1087 	register_t *retval;
1088 {
1089 	struct linux_sys___sysctl_args /* {
1090 		syscallarg(struct linux___sysctl *) lsp;
1091 	} */ *uap = v;
1092 	struct linux___sysctl ls;
1093 	struct sys___sysctl_args bsa;
1094 	int error;
1095 
1096 	if ((error = copyin(SCARG(uap, lsp), &ls, sizeof ls)))
1097 		return error;
1098 	SCARG(&bsa, name) = ls.name;
1099 	SCARG(&bsa, namelen) = ls.namelen;
1100 	SCARG(&bsa, old) = ls.old;
1101 	SCARG(&bsa, oldlenp) = ls.oldlenp;
1102 	SCARG(&bsa, new) = ls.new;
1103 	SCARG(&bsa, newlen) = ls.newlen;
1104 
1105 	return sys___sysctl(p, &bsa, retval);
1106 }
1107 
1108 int
1109 linux_sys_setresuid(p, v, retval)
1110 	struct proc *p;
1111 	void *v;
1112 	register_t *retval;
1113 {
1114 	struct linux_sys_setresuid_args /* {
1115 		syscallarg(uid_t) ruid;
1116 		syscallarg(uid_t) euid;
1117 		syscallarg(uid_t) suid;
1118 	} */ *uap = v;
1119 	struct pcred *pc = p->p_cred;
1120 	uid_t ruid, euid, suid;
1121 	int error;
1122 
1123 	ruid = SCARG(uap, ruid);
1124 	euid = SCARG(uap, euid);
1125 	suid = SCARG(uap, suid);
1126 
1127 	/*
1128 	 * Note: These checks are a little different than the NetBSD
1129 	 * setreuid(2) call performs.  This precisely follows the
1130 	 * behavior of the Linux kernel.
1131 	 */
1132 	if (ruid != (uid_t)-1 &&
1133 	    ruid != pc->p_ruid &&
1134 	    ruid != pc->pc_ucred->cr_uid &&
1135 	    ruid != pc->p_svuid &&
1136 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
1137 		return (error);
1138 
1139 	if (euid != (uid_t)-1 &&
1140 	    euid != pc->p_ruid &&
1141 	    euid != pc->pc_ucred->cr_uid &&
1142 	    euid != pc->p_svuid &&
1143 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
1144 		return (error);
1145 
1146 	if (suid != (uid_t)-1 &&
1147 	    suid != pc->p_ruid &&
1148 	    suid != pc->pc_ucred->cr_uid &&
1149 	    suid != pc->p_svuid &&
1150 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
1151 		return (error);
1152 
1153 	/*
1154 	 * Now assign the new real, effective, and saved UIDs.
1155 	 * Note that Linux, unlike NetBSD in setreuid(2), does not
1156 	 * set the saved UID in this call unless the user specifies
1157 	 * it.
1158 	 */
1159 	if (ruid != (uid_t)-1) {
1160 		(void)chgproccnt(pc->p_ruid, -1);
1161 		(void)chgproccnt(ruid, 1);
1162 		pc->p_ruid = ruid;
1163 	}
1164 
1165 	if (euid != (uid_t)-1) {
1166 		pc->pc_ucred = crcopy(pc->pc_ucred);
1167 		pc->pc_ucred->cr_uid = euid;
1168 	}
1169 
1170 	if (suid != (uid_t)-1)
1171 		pc->p_svuid = suid;
1172 
1173 	if (ruid != (uid_t)-1 && euid != (uid_t)-1 && suid != (uid_t)-1)
1174 		p->p_flag |= P_SUGID;
1175 	return (0);
1176 }
1177 
1178 int
1179 linux_sys_getresuid(p, v, retval)
1180 	struct proc *p;
1181 	void *v;
1182 	register_t *retval;
1183 {
1184 	struct linux_sys_getresuid_args /* {
1185 		syscallarg(uid_t *) ruid;
1186 		syscallarg(uid_t *) euid;
1187 		syscallarg(uid_t *) suid;
1188 	} */ *uap = v;
1189 	struct pcred *pc = p->p_cred;
1190 	int error;
1191 
1192 	/*
1193 	 * Linux copies these values out to userspace like so:
1194 	 *
1195 	 *	1. Copy out ruid.
1196 	 *	2. If that succeeds, copy out euid.
1197 	 *	3. If both of those succeed, copy out suid.
1198 	 */
1199 	if ((error = copyout(&pc->p_ruid, SCARG(uap, ruid),
1200 			     sizeof(uid_t))) != 0)
1201 		return (error);
1202 
1203 	if ((error = copyout(&pc->pc_ucred->cr_uid, SCARG(uap, euid),
1204 			     sizeof(uid_t))) != 0)
1205 		return (error);
1206 
1207 	return (copyout(&pc->p_svuid, SCARG(uap, suid), sizeof(uid_t)));
1208 }
1209 
1210 int
1211 linux_sys_ptrace(p, v, retval)
1212 	struct proc *p;
1213 	void *v;
1214 	register_t *retval;
1215 {
1216 	struct linux_sys_ptrace_args /* {
1217 		i386, m68k: T=int
1218 		alpha: T=long
1219 		syscallarg(T) request;
1220 		syscallarg(T) pid;
1221 		syscallarg(T) addr;
1222 		syscallarg(T) data;
1223 	} */ *uap = v;
1224 	const int *ptr;
1225 	int request;
1226 
1227 	ptr = linux_ptrace_request_map;
1228 	request = SCARG(uap, request);
1229 	while (*ptr != -1)
1230 		if (*ptr++ == request) {
1231 			struct sys_ptrace_args pta;
1232 			caddr_t sg;
1233 
1234 			sg = stackgap_init(p->p_emul);
1235 
1236 			SCARG(&pta, req) = *ptr;
1237 			SCARG(&pta, pid) = SCARG(uap, pid);
1238 			SCARG(&pta, addr) = (caddr_t)SCARG(uap, addr);
1239 			SCARG(&pta, data) = SCARG(uap, data);
1240 
1241 			/*
1242 			 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
1243 			 * to continue as the process left off previously,
1244 			 * i.e. same as if NetBSD ptrace called with
1245 			 * addr == (caddr_t) 1.
1246 			 */
1247 			if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
1248 				SCARG(&pta, addr) = (caddr_t) 1;
1249 
1250 			return sys_ptrace(p, &pta, retval);
1251 		}
1252 		else
1253 			ptr++;
1254 
1255 	return LINUX_SYS_PTRACE_ARCH(p, uap, retval);
1256 }
1257 
1258 int
1259 linux_sys_reboot(struct proc *p, void *v, register_t *retval)
1260 {
1261 	struct linux_sys_reboot_args /* {
1262 		syscallarg(int) magic1;
1263 		syscallarg(int) magic2;
1264 		syscallarg(int) cmd;
1265 		syscallarg(void *) arg;
1266 	} */ *uap = v;
1267 	struct sys_reboot_args /* {
1268 		syscallarg(int) opt;
1269 		syscallarg(char *) bootstr;
1270 	} */ sra;
1271 	int error;
1272 
1273 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1274 		return(error);
1275 
1276 	if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
1277 		return(EINVAL);
1278 	if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
1279 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
1280 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
1281 		return(EINVAL);
1282 
1283 	switch (SCARG(uap, cmd)) {
1284 	case LINUX_REBOOT_CMD_RESTART:
1285 		SCARG(&sra, opt) = RB_AUTOBOOT;
1286 		break;
1287 	case LINUX_REBOOT_CMD_HALT:
1288 		SCARG(&sra, opt) = RB_HALT;
1289 		break;
1290 	case LINUX_REBOOT_CMD_POWER_OFF:
1291 		SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
1292 		break;
1293 	case LINUX_REBOOT_CMD_RESTART2:
1294 		/* Reboot with an argument. */
1295 		SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
1296 		SCARG(&sra, bootstr) = SCARG(uap, arg);
1297 		break;
1298 	case LINUX_REBOOT_CMD_CAD_ON:
1299 		return(EINVAL);	/* We don't implement ctrl-alt-delete */
1300 	case LINUX_REBOOT_CMD_CAD_OFF:
1301 		return(0);
1302 	default:
1303 		return(EINVAL);
1304 	}
1305 
1306 	return(sys_reboot(p, &sra, retval));
1307 }
1308 
1309 /*
1310  * Copy of compat_12_sys_swapon().
1311  */
1312 int
1313 linux_sys_swapon(p, v, retval)
1314 	struct proc *p;
1315 	void *v;
1316 	register_t *retval;
1317 {
1318 	struct sys_swapctl_args ua;
1319 	struct linux_sys_swapon_args /* {
1320 		syscallarg(const char *) name;
1321 	} */ *uap = v;
1322 
1323 	SCARG(&ua, cmd) = SWAP_ON;
1324 	SCARG(&ua, arg) = (void *)SCARG(uap, name);
1325 	SCARG(&ua, misc) = 0;	/* priority */
1326 	return (sys_swapctl(p, &ua, retval));
1327 }
1328 
1329 /*
1330  * Stop swapping to the file or block device specified by path.
1331  */
1332 int
1333 linux_sys_swapoff(p, v, retval)
1334 	struct proc *p;
1335 	void *v;
1336 	register_t *retval;
1337 {
1338 	struct sys_swapctl_args ua;
1339 	struct linux_sys_swapoff_args /* {
1340 		syscallarg(const char *) path;
1341 	} */ *uap = v;
1342 
1343 	SCARG(&ua, cmd) = SWAP_OFF;
1344 	SCARG(&ua, arg) = (void *)SCARG(uap, path);
1345 	return (sys_swapctl(p, &ua, retval));
1346 }
1347 
1348 /*
1349  * Copy of compat_09_sys_setdomainname()
1350  */
1351 /* ARGSUSED */
1352 int
1353 linux_sys_setdomainname(p, v, retval)
1354 	struct proc *p;
1355 	void *v;
1356 	register_t *retval;
1357 {
1358 	struct linux_sys_setdomainname_args /* {
1359 		syscallarg(char *) domainname;
1360 		syscallarg(int) len;
1361 	} */ *uap = v;
1362 	int name;
1363 	int error;
1364 
1365 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1366 		return (error);
1367 	name = KERN_DOMAINNAME;
1368 	return (kern_sysctl(&name, 1, 0, 0, SCARG(uap, domainname),
1369 			    SCARG(uap, len), p));
1370 }
1371 
1372 /*
1373  * sysinfo()
1374  */
1375 /* ARGSUSED */
1376 int
1377 linux_sys_sysinfo(p, v, retval)
1378 	struct proc *p;
1379 	void *v;
1380 	register_t *retval;
1381 {
1382 	struct linux_sys_sysinfo_args /* {
1383 		syscallarg(struct linux_sysinfo *) arg;
1384 	} */ *uap = v;
1385 	struct linux_sysinfo si;
1386 	struct loadavg *la;
1387 
1388 	si.uptime = time.tv_sec - boottime.tv_sec;
1389 	la = &averunnable;
1390 	si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1391 	si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1392 	si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1393 	si.totalram = ctob(physmem);
1394 	si.freeram = uvmexp.free * uvmexp.pagesize;
1395 	si.sharedram = 0;	/* XXX */
1396 	si.bufferram = uvmexp.vnodepages * uvmexp.pagesize;
1397 	si.totalswap = uvmexp.swpages * uvmexp.pagesize;
1398 	si.freeswap = (uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
1399 	si.procs = nprocs;
1400 
1401 	/* The following are only present in newer Linux kernels. */
1402 	si.totalbig = 0;
1403 	si.freebig = 0;
1404 	si.mem_unit = 1;
1405 
1406 	return (copyout(&si, SCARG(uap, arg), sizeof si));
1407 }
1408 
1409 /*
1410  * This gets called for unsupported syscalls. The difference to sys_nosys()
1411  * is that process does not get SIGSYS, the call just returns with ENOSYS.
1412  * This is the way Linux does it and glibc depends on this behaviour.
1413  */
1414 int
1415 linux_sys_nosys(p, v, retval)
1416 	struct proc *p;
1417 	void *v;
1418 	register_t *retval;
1419 {
1420 	return (ENOSYS);
1421 }
1422