xref: /netbsd-src/sys/compat/linux/common/linux_misc.c (revision e4d7c2e329d54c97e0c0bd3016bbe74f550c3d5e)
1 /*	$NetBSD: linux_misc.c,v 1.64 2000/02/17 12:34:26 abs 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/ptrace.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 
94 #include <sys/syscallargs.h>
95 
96 #include <vm/vm.h>
97 #include <vm/vm_param.h>
98 
99 #include <compat/linux/common/linux_types.h>
100 #include <compat/linux/common/linux_signal.h>
101 
102 #include <compat/linux/linux_syscallargs.h>
103 
104 #include <compat/linux/common/linux_fcntl.h>
105 #include <compat/linux/common/linux_mmap.h>
106 #include <compat/linux/common/linux_dirent.h>
107 #include <compat/linux/common/linux_util.h>
108 #include <compat/linux/common/linux_misc.h>
109 #include <compat/linux/common/linux_ptrace.h>
110 
111 int linux_ptrace_request_map[] = {
112 	LINUX_PTRACE_TRACEME,	PT_TRACE_ME,
113 	LINUX_PTRACE_PEEKTEXT,	PT_READ_I,
114 	LINUX_PTRACE_PEEKDATA,	PT_READ_D,
115 	LINUX_PTRACE_POKETEXT,	PT_WRITE_I,
116 	LINUX_PTRACE_POKEDATA,	PT_WRITE_D,
117 	LINUX_PTRACE_CONT,	PT_CONTINUE,
118 	LINUX_PTRACE_KILL,	PT_KILL,
119 	LINUX_PTRACE_ATTACH,	PT_ATTACH,
120 	LINUX_PTRACE_DETACH,	PT_DETACH,
121 	-1
122 };
123 
124 /* Local linux_misc.c functions: */
125 static void bsd_to_linux_statfs __P((struct statfs *, struct linux_statfs *));
126 
127 /*
128  * The information on a terminated (or stopped) process needs
129  * to be converted in order for Linux binaries to get a valid signal
130  * number out of it.
131  */
132 void
133 bsd_to_linux_wstat(st)
134 	int *st;
135 {
136 
137 	int sig;
138 
139 	if (WIFSIGNALED(*st)) {
140 		sig = WTERMSIG(*st);
141 		if (sig >= 0 && sig < NSIG)
142 			*st= (*st& ~0177) | native_to_linux_sig[sig];
143 	} else if (WIFSTOPPED(*st)) {
144 		sig = WSTOPSIG(*st);
145 		if (sig >= 0 && sig < NSIG)
146 			*st = (*st & ~0xff00) | (native_to_linux_sig[sig] << 8);
147 	}
148 }
149 
150 /*
151  * This is very much the same as waitpid()
152  */
153 int
154 linux_sys_wait4(p, v, retval)
155 	struct proc *p;
156 	void *v;
157 	register_t *retval;
158 {
159 	struct linux_sys_wait4_args /* {
160 		syscallarg(int) pid;
161 		syscallarg(int *) status;
162 		syscallarg(int) options;
163 		syscallarg(struct rusage *) rusage;
164 	} */ *uap = v;
165 	struct sys_wait4_args w4a;
166 	int error, *status, tstat, options, linux_options;
167 	caddr_t sg;
168 
169 	if (SCARG(uap, status) != NULL) {
170 		sg = stackgap_init(p->p_emul);
171 		status = (int *) stackgap_alloc(&sg, sizeof *status);
172 	} else
173 		status = NULL;
174 
175 	linux_options = SCARG(uap, options);
176 	options = 0;
177 	if (linux_options &
178 	    ~(LINUX_WAIT4_WNOHANG|LINUX_WAIT4_WUNTRACED|LINUX_WAIT4_WCLONE))
179 		return (EINVAL);
180 
181 	if (linux_options & LINUX_WAIT4_WNOHANG)
182 		options |= WNOHANG;
183 	if (linux_options & LINUX_WAIT4_WUNTRACED)
184 		options |= WUNTRACED;
185 	if (linux_options & LINUX_WAIT4_WCLONE)
186 		options |= WALTSIG;
187 
188 	SCARG(&w4a, pid) = SCARG(uap, pid);
189 	SCARG(&w4a, status) = status;
190 	SCARG(&w4a, options) = options;
191 	SCARG(&w4a, rusage) = SCARG(uap, rusage);
192 
193 	if ((error = sys_wait4(p, &w4a, retval)))
194 		return error;
195 
196 	sigdelset(&p->p_siglist, SIGCHLD);
197 
198 	if (status != NULL) {
199 		if ((error = copyin(status, &tstat, sizeof tstat)))
200 			return error;
201 
202 		bsd_to_linux_wstat(&tstat);
203 		return copyout(&tstat, SCARG(uap, status), sizeof tstat);
204 	}
205 
206 	return 0;
207 }
208 
209 /*
210  * Linux brk(2). The check if the new address is >= the old one is
211  * done in the kernel in Linux. NetBSD does it in the library.
212  */
213 int
214 linux_sys_brk(p, v, retval)
215 	struct proc *p;
216 	void *v;
217 	register_t *retval;
218 {
219 	struct linux_sys_brk_args /* {
220 		syscallarg(char *) nsize;
221 	} */ *uap = v;
222 	char *nbrk = SCARG(uap, nsize);
223 	struct sys_obreak_args oba;
224 	struct vmspace *vm = p->p_vmspace;
225 	caddr_t oldbrk;
226 
227 	oldbrk = vm->vm_daddr + ctob(vm->vm_dsize);
228 	/*
229 	 * XXX inconsistent.. Linux always returns at least the old
230 	 * brk value, but it will be page-aligned if this fails,
231 	 * and possibly not page aligned if it succeeds (the user
232 	 * supplied pointer is returned).
233 	 */
234 	SCARG(&oba, nsize) = nbrk;
235 
236 	if ((caddr_t) nbrk > vm->vm_daddr && sys_obreak(p, &oba, retval) == 0)
237 		retval[0] = (register_t)nbrk;
238 	else
239 		retval[0] = (register_t)oldbrk;
240 
241 	return 0;
242 }
243 
244 /*
245  * Convert BSD statfs structure to Linux statfs structure.
246  * The Linux structure has less fields, and it also wants
247  * the length of a name in a dir entry in a field, which
248  * we fake (probably the wrong way).
249  */
250 static void
251 bsd_to_linux_statfs(bsp, lsp)
252 	struct statfs *bsp;
253 	struct linux_statfs *lsp;
254 {
255 
256 	lsp->l_ftype = bsp->f_type;
257 	lsp->l_fbsize = bsp->f_bsize;
258 	lsp->l_fblocks = bsp->f_blocks;
259 	lsp->l_fbfree = bsp->f_bfree;
260 	lsp->l_fbavail = bsp->f_bavail;
261 	lsp->l_ffiles = bsp->f_files;
262 	lsp->l_fffree = bsp->f_ffree;
263 	lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
264 	lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
265 	lsp->l_fnamelen = MAXNAMLEN;	/* XXX */
266 }
267 
268 /*
269  * Implement the fs stat functions. Straightforward.
270  */
271 int
272 linux_sys_statfs(p, v, retval)
273 	struct proc *p;
274 	void *v;
275 	register_t *retval;
276 {
277 	struct linux_sys_statfs_args /* {
278 		syscallarg(const char *) path;
279 		syscallarg(struct linux_statfs *) sp;
280 	} */ *uap = v;
281 	struct statfs btmp, *bsp;
282 	struct linux_statfs ltmp;
283 	struct sys_statfs_args bsa;
284 	caddr_t sg;
285 	int error;
286 
287 	sg = stackgap_init(p->p_emul);
288 	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
289 
290 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
291 
292 	SCARG(&bsa, path) = SCARG(uap, path);
293 	SCARG(&bsa, buf) = bsp;
294 
295 	if ((error = sys_statfs(p, &bsa, retval)))
296 		return error;
297 
298 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
299 		return error;
300 
301 	bsd_to_linux_statfs(&btmp, &ltmp);
302 
303 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
304 }
305 
306 int
307 linux_sys_fstatfs(p, v, retval)
308 	struct proc *p;
309 	void *v;
310 	register_t *retval;
311 {
312 	struct linux_sys_fstatfs_args /* {
313 		syscallarg(int) fd;
314 		syscallarg(struct linux_statfs *) sp;
315 	} */ *uap = v;
316 	struct statfs btmp, *bsp;
317 	struct linux_statfs ltmp;
318 	struct sys_fstatfs_args bsa;
319 	caddr_t sg;
320 	int error;
321 
322 	sg = stackgap_init(p->p_emul);
323 	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
324 
325 	SCARG(&bsa, fd) = SCARG(uap, fd);
326 	SCARG(&bsa, buf) = bsp;
327 
328 	if ((error = sys_fstatfs(p, &bsa, retval)))
329 		return error;
330 
331 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
332 		return error;
333 
334 	bsd_to_linux_statfs(&btmp, &ltmp);
335 
336 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
337 }
338 
339 /*
340  * uname(). Just copy the info from the various strings stored in the
341  * kernel, and put it in the Linux utsname structure. That structure
342  * is almost the same as the NetBSD one, only it has fields 65 characters
343  * long, and an extra domainname field.
344  */
345 int
346 linux_sys_uname(p, v, retval)
347 	struct proc *p;
348 	void *v;
349 	register_t *retval;
350 {
351 	struct linux_sys_uname_args /* {
352 		syscallarg(struct linux_utsname *) up;
353 	} */ *uap = v;
354 	extern char ostype[], hostname[], osrelease[], version[], machine[],
355 	    domainname[];
356 	struct linux_utsname luts;
357 	int len;
358 	char *cp;
359 
360 	strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
361 	strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
362 	strncpy(luts.l_release, osrelease, sizeof(luts.l_release));
363 	strncpy(luts.l_version, version, sizeof(luts.l_version));
364 	strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
365 	strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
366 
367 	/* This part taken from the the uname() in libc */
368 	len = sizeof(luts.l_version);
369 	for (cp = luts.l_version; len--; ++cp) {
370 		if (*cp == '\n' || *cp == '\t') {
371 			if (len > 1)
372 				*cp = ' ';
373 			else
374 				*cp = '\0';
375 		}
376 	}
377 
378 	return copyout(&luts, SCARG(uap, up), sizeof(luts));
379 }
380 
381 /* Used directly on: alpha, mips, ppc, sparc, sparc64 */
382 /* Used indirectly on: arm, i386, m68k */
383 
384 /*
385  * New type Linux mmap call.
386  * Only called directly on machines with >= 6 free regs.
387  */
388 int
389 linux_sys_mmap(p, v, retval)
390 	struct proc *p;
391 	void *v;
392 	register_t *retval;
393 {
394 	struct linux_sys_mmap_args /* {
395 		syscallarg(unsigned long) addr;
396 		syscallarg(size_t) len;
397 		syscallarg(int) prot;
398 		syscallarg(int) flags;
399 		syscallarg(int) fd;
400 		syscallarg(off_t) offset;
401 	} */ *uap = v;
402 	struct sys_mmap_args cma;
403 	int flags;
404 
405 	flags = 0;
406 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_SHARED, MAP_SHARED);
407 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_PRIVATE, MAP_PRIVATE);
408 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_FIXED, MAP_FIXED);
409 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_ANON, MAP_ANON);
410 	/* XXX XAX ERH: Any other flags here?  There are more defined... */
411 
412 	SCARG(&cma,addr) = (void *)SCARG(uap, addr);
413 	SCARG(&cma,len) = SCARG(uap, len);
414 	SCARG(&cma,prot) = SCARG(uap, prot);
415 	if (SCARG(&cma,prot) & VM_PROT_WRITE) /* XXX */
416 		SCARG(&cma,prot) |= VM_PROT_READ;
417 	SCARG(&cma,flags) = flags;
418 	SCARG(&cma,fd) = SCARG(uap, fd);
419 	SCARG(&cma,pad) = 0;
420 	SCARG(&cma,pos) = SCARG(uap, offset);
421 
422 	return sys_mmap(p, &cma, retval);
423 }
424 
425 int
426 linux_sys_mremap(p, v, retval)
427 	struct proc *p;
428 	void *v;
429 	register_t *retval;
430 {
431 	struct linux_sys_mremap_args /* {
432 		syscallarg(void *) old_address;
433 		syscallarg(size_t) old_size;
434 		syscallarg(size_t) new_size;
435 		syscallarg(u_long) flags;
436 	} */ *uap = v;
437 	struct sys_munmap_args mua;
438 	size_t old_size, new_size;
439 	int error;
440 
441 	old_size = round_page(SCARG(uap, old_size));
442 	new_size = round_page(SCARG(uap, new_size));
443 
444 	/*
445 	 * Growing mapped region.
446 	 */
447 	if (new_size > old_size) {
448 		/*
449 		 * XXX Implement me.  What we probably want to do is
450 		 * XXX dig out the guts of the old mapping, mmap that
451 		 * XXX object again with the new size, then munmap
452 		 * XXX the old mapping.
453 		 */
454 		*retval = 0;
455 		return (ENOMEM);
456 	}
457 
458 	/*
459 	 * Shrinking mapped region.
460 	 */
461 	if (new_size < old_size) {
462 		SCARG(&mua, addr) = (caddr_t)SCARG(uap, old_address) +
463 		    new_size;
464 		SCARG(&mua, len) = old_size - new_size;
465 		error = sys_munmap(p, &mua, retval);
466 		*retval = error ? 0 : (register_t)SCARG(uap, old_address);
467 		return (error);
468 	}
469 
470 	/*
471 	 * No change.
472 	 */
473 	*retval = (register_t)SCARG(uap, old_address);
474 	return (0);
475 }
476 
477 int
478 linux_sys_msync(p, v, retval)
479 	struct proc *p;
480 	void *v;
481 	register_t *retval;
482 {
483 	struct linux_sys_msync_args /* {
484 		syscallarg(caddr_t) addr;
485 		syscallarg(int) len;
486 		syscallarg(int) fl;
487 	} */ *uap = v;
488 
489 	struct sys___msync13_args bma;
490 
491 	/* flags are ignored */
492 	SCARG(&bma, addr) = SCARG(uap, addr);
493 	SCARG(&bma, len) = SCARG(uap, len);
494 	SCARG(&bma, flags) = SCARG(uap, fl);
495 
496 	return sys___msync13(p, &bma, retval);
497 }
498 
499 /*
500  * This code is partly stolen from src/lib/libc/compat-43/times.c
501  * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
502  */
503 
504 #define CLK_TCK 100
505 #define	CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
506 
507 int
508 linux_sys_times(p, v, retval)
509 	struct proc *p;
510 	void *v;
511 	register_t *retval;
512 {
513 	struct linux_sys_times_args /* {
514 		syscallarg(struct times *) tms;
515 	} */ *uap = v;
516 	struct timeval t;
517 	struct linux_tms ltms;
518 	struct rusage ru;
519 	int error, s;
520 
521 	calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
522 	ltms.ltms_utime = CONVTCK(ru.ru_utime);
523 	ltms.ltms_stime = CONVTCK(ru.ru_stime);
524 
525 	ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
526 	ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
527 
528 	if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
529 		return error;
530 
531 	s = splclock();
532 	timersub(&time, &boottime, &t);
533 	splx(s);
534 
535 	retval[0] = ((linux_clock_t)(CONVTCK(t)));
536 	return 0;
537 }
538 
539 /*
540  * Linux 'readdir' call. This code is mostly taken from the
541  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
542  * an attempt has been made to keep it a little cleaner (failing
543  * miserably, because of the cruft needed if count 1 is passed).
544  *
545  * The d_off field should contain the offset of the next valid entry,
546  * but in Linux it has the offset of the entry itself. We emulate
547  * that bug here.
548  *
549  * Read in BSD-style entries, convert them, and copy them out.
550  *
551  * Note that this doesn't handle union-mounted filesystems.
552  */
553 int
554 linux_sys_getdents(p, v, retval)
555 	struct proc *p;
556 	void *v;
557 	register_t *retval;
558 {
559 	struct linux_sys_getdents_args /* {
560 		syscallarg(int) fd;
561 		syscallarg(struct linux_dirent *) dent;
562 		syscallarg(unsigned int) count;
563 	} */ *uap = v;
564 	register struct dirent *bdp;
565 	struct vnode *vp;
566 	caddr_t	inp, buf;		/* BSD-format */
567 	int len, reclen;		/* BSD-format */
568 	caddr_t outp;			/* Linux-format */
569 	int resid, linux_reclen = 0;	/* Linux-format */
570 	struct file *fp;
571 	struct uio auio;
572 	struct iovec aiov;
573 	struct linux_dirent idb;
574 	off_t off;		/* true file offset */
575 	int buflen, error, eofflag, nbytes, oldcall;
576 	struct vattr va;
577 	off_t *cookiebuf = NULL, *cookie;
578 	int ncookies;
579 
580 	/* getvnode() will use the descriptor for us */
581 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
582 		return (error);
583 
584 	if ((fp->f_flag & FREAD) == 0) {
585 		error = EBADF;
586 		goto out1;
587 	}
588 
589 	vp = (struct vnode *)fp->f_data;
590 	if (vp->v_type != VDIR) {
591 		error = EINVAL;
592 		goto out1;
593 	}
594 
595 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
596 		goto out1;
597 
598 	nbytes = SCARG(uap, count);
599 	if (nbytes == 1) {	/* emulating old, broken behaviour */
600 		nbytes = sizeof (struct linux_dirent);
601 		buflen = max(va.va_blocksize, nbytes);
602 		oldcall = 1;
603 	} else {
604 		buflen = min(MAXBSIZE, nbytes);
605 		if (buflen < va.va_blocksize)
606 			buflen = va.va_blocksize;
607 		oldcall = 0;
608 	}
609 	buf = malloc(buflen, M_TEMP, M_WAITOK);
610 
611 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
612 	off = fp->f_offset;
613 again:
614 	aiov.iov_base = buf;
615 	aiov.iov_len = buflen;
616 	auio.uio_iov = &aiov;
617 	auio.uio_iovcnt = 1;
618 	auio.uio_rw = UIO_READ;
619 	auio.uio_segflg = UIO_SYSSPACE;
620 	auio.uio_procp = p;
621 	auio.uio_resid = buflen;
622 	auio.uio_offset = off;
623 	/*
624          * First we read into the malloc'ed buffer, then
625          * we massage it into user space, one record at a time.
626          */
627 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
628 	    &ncookies);
629 	if (error)
630 		goto out;
631 
632 	inp = buf;
633 	outp = (caddr_t)SCARG(uap, dent);
634 	resid = nbytes;
635 	if ((len = buflen - auio.uio_resid) == 0)
636 		goto eof;
637 
638 	for (cookie = cookiebuf; len > 0; len -= reclen) {
639 		bdp = (struct dirent *)inp;
640 		reclen = bdp->d_reclen;
641 		if (reclen & 3)
642 			panic("linux_readdir");
643 		if (bdp->d_fileno == 0) {
644 			inp += reclen;	/* it is a hole; squish it out */
645 			off = *cookie++;
646 			continue;
647 		}
648 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
649 		if (reclen > len || resid < linux_reclen) {
650 			/* entry too big for buffer, so just stop */
651 			outp++;
652 			break;
653 		}
654 		/*
655 		 * Massage in place to make a Linux-shaped dirent (otherwise
656 		 * we have to worry about touching user memory outside of
657 		 * the copyout() call).
658 		 */
659 		idb.d_ino = (linux_ino_t)bdp->d_fileno;
660 		/*
661 		 * The old readdir() call misuses the offset and reclen fields.
662 		 */
663 		if (oldcall) {
664 			idb.d_off = (linux_off_t)linux_reclen;
665 			idb.d_reclen = (u_short)bdp->d_namlen;
666 		} else {
667 			if (sizeof (linux_off_t) < 4 && (off >> 32) != 0) {
668 				compat_offseterr(vp, "linux_getdents");
669 				error = EINVAL;
670 				goto out;
671 			}
672 			idb.d_off = (linux_off_t)off;
673 			idb.d_reclen = (u_short)linux_reclen;
674 		}
675 		strcpy(idb.d_name, bdp->d_name);
676 		if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
677 			goto out;
678 		/* advance past this real entry */
679 		inp += reclen;
680 		off = *cookie++;	/* each entry points to itself */
681 		/* advance output past Linux-shaped entry */
682 		outp += linux_reclen;
683 		resid -= linux_reclen;
684 		if (oldcall)
685 			break;
686 	}
687 
688 	/* if we squished out the whole block, try again */
689 	if (outp == (caddr_t)SCARG(uap, dent))
690 		goto again;
691 	fp->f_offset = off;	/* update the vnode offset */
692 
693 	if (oldcall)
694 		nbytes = resid + linux_reclen;
695 
696 eof:
697 	*retval = nbytes - resid;
698 out:
699 	VOP_UNLOCK(vp, 0);
700 	if (cookiebuf)
701 		free(cookiebuf, M_TEMP);
702 	free(buf, M_TEMP);
703  out1:
704 	FILE_UNUSE(fp, p);
705 	return error;
706 }
707 
708 /*
709  * Even when just using registers to pass arguments to syscalls you can
710  * have 5 of them on the i386. So this newer version of select() does
711  * this.
712  */
713 int
714 linux_sys_select(p, v, retval)
715 	struct proc *p;
716 	void *v;
717 	register_t *retval;
718 {
719 	struct linux_sys_select_args /* {
720 		syscallarg(int) nfds;
721 		syscallarg(fd_set *) readfds;
722 		syscallarg(fd_set *) writefds;
723 		syscallarg(fd_set *) exceptfds;
724 		syscallarg(struct timeval *) timeout;
725 	} */ *uap = v;
726 
727 	return linux_select1(p, retval, SCARG(uap, nfds), SCARG(uap, readfds),
728 	    SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
729 }
730 
731 /*
732  * Common code for the old and new versions of select(). A couple of
733  * things are important:
734  * 1) return the amount of time left in the 'timeout' parameter
735  * 2) select never returns ERESTART on Linux, always return EINTR
736  */
737 int
738 linux_select1(p, retval, nfds, readfds, writefds, exceptfds, timeout)
739 	struct proc *p;
740 	register_t *retval;
741 	int nfds;
742 	fd_set *readfds, *writefds, *exceptfds;
743 	struct timeval *timeout;
744 {
745 	struct sys_select_args bsa;
746 	struct timeval tv0, tv1, utv, *tvp;
747 	caddr_t sg;
748 	int error;
749 
750 	SCARG(&bsa, nd) = nfds;
751 	SCARG(&bsa, in) = readfds;
752 	SCARG(&bsa, ou) = writefds;
753 	SCARG(&bsa, ex) = exceptfds;
754 	SCARG(&bsa, tv) = timeout;
755 
756 	/*
757 	 * Store current time for computation of the amount of
758 	 * time left.
759 	 */
760 	if (timeout) {
761 		if ((error = copyin(timeout, &utv, sizeof(utv))))
762 			return error;
763 		if (itimerfix(&utv)) {
764 			/*
765 			 * The timeval was invalid.  Convert it to something
766 			 * valid that will act as it does under Linux.
767 			 */
768 			sg = stackgap_init(p->p_emul);
769 			tvp = stackgap_alloc(&sg, sizeof(utv));
770 			utv.tv_sec += utv.tv_usec / 1000000;
771 			utv.tv_usec %= 1000000;
772 			if (utv.tv_usec < 0) {
773 				utv.tv_sec -= 1;
774 				utv.tv_usec += 1000000;
775 			}
776 			if (utv.tv_sec < 0)
777 				timerclear(&utv);
778 			if ((error = copyout(&utv, tvp, sizeof(utv))))
779 				return error;
780 			SCARG(&bsa, tv) = tvp;
781 		}
782 		microtime(&tv0);
783 	}
784 
785 	error = sys_select(p, &bsa, retval);
786 	if (error) {
787 		/*
788 		 * See fs/select.c in the Linux kernel.  Without this,
789 		 * Maelstrom doesn't work.
790 		 */
791 		if (error == ERESTART)
792 			error = EINTR;
793 		return error;
794 	}
795 
796 	if (timeout) {
797 		if (*retval) {
798 			/*
799 			 * Compute how much time was left of the timeout,
800 			 * by subtracting the current time and the time
801 			 * before we started the call, and subtracting
802 			 * that result from the user-supplied value.
803 			 */
804 			microtime(&tv1);
805 			timersub(&tv1, &tv0, &tv1);
806 			timersub(&utv, &tv1, &utv);
807 			if (utv.tv_sec < 0)
808 				timerclear(&utv);
809 		} else
810 			timerclear(&utv);
811 		if ((error = copyout(&utv, timeout, sizeof(utv))))
812 			return error;
813 	}
814 
815 	return 0;
816 }
817 
818 /*
819  * Get the process group of a certain process. Look it up
820  * and return the value.
821  */
822 int
823 linux_sys_getpgid(p, v, retval)
824 	struct proc *p;
825 	void *v;
826 	register_t *retval;
827 {
828 	struct linux_sys_getpgid_args /* {
829 		syscallarg(int) pid;
830 	} */ *uap = v;
831 	struct proc *targp;
832 
833 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
834 		if ((targp = pfind(SCARG(uap, pid))) == 0)
835 			return ESRCH;
836 	}
837 	else
838 		targp = p;
839 
840 	retval[0] = targp->p_pgid;
841 	return 0;
842 }
843 
844 /*
845  * Set the 'personality' (emulation mode) for the current process. Only
846  * accept the Linux personality here (0). This call is needed because
847  * the Linux ELF crt0 issues it in an ugly kludge to make sure that
848  * ELF binaries run in Linux mode, not SVR4 mode.
849  */
850 int
851 linux_sys_personality(p, v, retval)
852 	struct proc *p;
853 	void *v;
854 	register_t *retval;
855 {
856 	struct linux_sys_personality_args /* {
857 		syscallarg(int) per;
858 	} */ *uap = v;
859 
860 	if (SCARG(uap, per) != 0)
861 		return EINVAL;
862 	retval[0] = 0;
863 	return 0;
864 }
865 
866 /*
867  * The calls are here because of type conversions.
868  */
869 int
870 linux_sys_setreuid(p, v, retval)
871 	struct proc *p;
872 	void *v;
873 	register_t *retval;
874 {
875 	struct linux_sys_setreuid_args /* {
876 		syscallarg(int) ruid;
877 		syscallarg(int) euid;
878 	} */ *uap = v;
879 	struct sys_setreuid_args bsa;
880 
881 	SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
882 		(uid_t)-1 : SCARG(uap, ruid);
883 	SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
884 		(uid_t)-1 : SCARG(uap, euid);
885 
886 	return sys_setreuid(p, &bsa, retval);
887 }
888 
889 int
890 linux_sys_setregid(p, v, retval)
891 	struct proc *p;
892 	void *v;
893 	register_t *retval;
894 {
895 	struct linux_sys_setregid_args /* {
896 		syscallarg(int) rgid;
897 		syscallarg(int) egid;
898 	} */ *uap = v;
899 	struct sys_setregid_args bsa;
900 
901 	SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
902 		(uid_t)-1 : SCARG(uap, rgid);
903 	SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
904 		(uid_t)-1 : SCARG(uap, egid);
905 
906 	return sys_setregid(p, &bsa, retval);
907 }
908 
909 /*
910  * We have nonexistent fsuid equal to uid.
911  * If modification is requested, refuse.
912  */
913 int
914 linux_sys_setfsuid(p, v, retval)
915 	 struct proc *p;
916 	 void *v;
917 	 register_t *retval;
918 {
919 	 struct linux_sys_setfsuid_args /* {
920 		 syscallarg(uid_t) uid;
921 	 } */ *uap = v;
922 	 uid_t uid;
923 
924 	 uid = SCARG(uap, uid);
925 	 if (p->p_cred->p_ruid != uid)
926 		 return sys_nosys(p, v, retval);
927 	 else
928 		 return (0);
929 }
930 
931 int
932 linux_sys_getfsuid(p, v, retval)
933 	 struct proc *p;
934 	 void *v;
935 	 register_t *retval;
936 {
937 	 return sys_getuid(p, v, retval);
938 }
939 
940 int
941 linux_sys___sysctl(p, v, retval)
942 	struct proc *p;
943 	void *v;
944 	register_t *retval;
945 {
946 	struct linux_sys___sysctl_args /* {
947 		syscallarg(struct linux___sysctl *) lsp;
948 	} */ *uap = v;
949 	struct linux___sysctl ls;
950 	struct sys___sysctl_args bsa;
951 	int error;
952 
953 	if ((error = copyin(SCARG(uap, lsp), &ls, sizeof ls)))
954 		return error;
955 	SCARG(&bsa, name) = ls.name;
956 	SCARG(&bsa, namelen) = ls.namelen;
957 	SCARG(&bsa, old) = ls.old;
958 	SCARG(&bsa, oldlenp) = ls.oldlenp;
959 	SCARG(&bsa, new) = ls.new;
960 	SCARG(&bsa, newlen) = ls.newlen;
961 
962 	return sys___sysctl(p, &bsa, retval);
963 }
964 
965 int
966 linux_sys_setresuid(p, v, retval)
967 	struct proc *p;
968 	void *v;
969 	register_t *retval;
970 {
971 	struct linux_sys_setresuid_args /* {
972 		syscallarg(uid_t) ruid;
973 		syscallarg(uid_t) euid;
974 		syscallarg(uid_t) suid;
975 	} */ *uap = v;
976 	struct pcred *pc = p->p_cred;
977 	uid_t ruid, euid, suid;
978 	int error;
979 
980 	ruid = SCARG(uap, ruid);
981 	euid = SCARG(uap, euid);
982 	suid = SCARG(uap, suid);
983 
984 	/*
985 	 * Note: These checks are a little different than the NetBSD
986 	 * setreuid(2) call performs.  This precisely follows the
987 	 * behavior of the Linux kernel.
988 	 */
989 	if (ruid != (uid_t)-1 &&
990 	    ruid != pc->p_ruid &&
991 	    ruid != pc->pc_ucred->cr_uid &&
992 	    ruid != pc->p_svuid &&
993 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
994 		return (error);
995 
996 	if (euid != (uid_t)-1 &&
997 	    euid != pc->p_ruid &&
998 	    euid != pc->pc_ucred->cr_uid &&
999 	    euid != pc->p_svuid &&
1000 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
1001 		return (error);
1002 
1003 	if (suid != (uid_t)-1 &&
1004 	    suid != pc->p_ruid &&
1005 	    suid != pc->pc_ucred->cr_uid &&
1006 	    suid != pc->p_svuid &&
1007 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
1008 		return (error);
1009 
1010 	/*
1011 	 * Now assign the new real, effective, and saved UIDs.
1012 	 * Note that Linux, unlike NetBSD in setreuid(2), does not
1013 	 * set the saved UID in this call unless the user specifies
1014 	 * it.
1015 	 */
1016 	if (ruid != (uid_t)-1) {
1017 		(void)chgproccnt(pc->p_ruid, -1);
1018 		(void)chgproccnt(ruid, 1);
1019 		pc->p_ruid = ruid;
1020 	}
1021 
1022 	if (euid != (uid_t)-1) {
1023 		pc->pc_ucred = crcopy(pc->pc_ucred);
1024 		pc->pc_ucred->cr_uid = euid;
1025 	}
1026 
1027 	if (suid != (uid_t)-1)
1028 		pc->p_svuid = suid;
1029 
1030 	if (ruid != (uid_t)-1 && euid != (uid_t)-1 && suid != (uid_t)-1)
1031 		p->p_flag |= P_SUGID;
1032 	return (0);
1033 }
1034 
1035 int
1036 linux_sys_getresuid(p, v, retval)
1037 	struct proc *p;
1038 	void *v;
1039 	register_t *retval;
1040 {
1041 	struct linux_sys_getresuid_args /* {
1042 		syscallarg(uid_t *) ruid;
1043 		syscallarg(uid_t *) euid;
1044 		syscallarg(uid_t *) suid;
1045 	} */ *uap = v;
1046 	struct pcred *pc = p->p_cred;
1047 	int error;
1048 
1049 	/*
1050 	 * Linux copies these values out to userspace like so:
1051 	 *
1052 	 *	1. Copy out ruid.
1053 	 *	2. If that succeeds, copy out euid.
1054 	 *	3. If both of those succeed, copy out suid.
1055 	 */
1056 	if ((error = copyout(&pc->p_ruid, SCARG(uap, ruid),
1057 			     sizeof(uid_t))) != 0)
1058 		return (error);
1059 
1060 	if ((error = copyout(&pc->pc_ucred->cr_uid, SCARG(uap, euid),
1061 			     sizeof(uid_t))) != 0)
1062 		return (error);
1063 
1064 	return (copyout(&pc->p_svuid, SCARG(uap, suid), sizeof(uid_t)));
1065 }
1066 
1067 int
1068 linux_sys_ptrace(p, v, retval)
1069 	struct proc *p;
1070 	void *v;
1071 	register_t *retval;
1072 {
1073 	struct linux_sys_ptrace_args /* {
1074 		syscallarg(int) request;
1075 		syscallarg(int) pid;
1076 		syscallarg(int) addr;
1077 		syscallarg(int) data;
1078 	} */ *uap = v;
1079 	int *ptr, request;
1080 
1081 	ptr = linux_ptrace_request_map;
1082 	request = SCARG(uap, request);
1083 	while (*ptr != -1)
1084 		if (*ptr++ == request) {
1085 			struct sys_ptrace_args pta;
1086 			caddr_t sg;
1087 
1088 			sg = stackgap_init(p->p_emul);
1089 
1090 			SCARG(&pta, req) = *ptr;
1091 			SCARG(&pta, pid) = SCARG(uap, pid);
1092 			SCARG(&pta, addr) = (caddr_t)SCARG(uap, addr);
1093 			SCARG(&pta, data) = SCARG(uap, data);
1094 
1095 			return sys_ptrace(p, &pta, retval);
1096 		}
1097 		else
1098 			ptr++;
1099 
1100 	return LINUX_SYS_PTRACE_ARCH(p, uap, retval);
1101 }
1102