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