xref: /netbsd-src/sys/compat/linux/common/linux_misc.c (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1 /*	$NetBSD: linux_misc.c,v 1.121 2003/06/29 22:29:30 fvdl 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/cdefs.h>
67 __KERNEL_RCSID(0, "$NetBSD: linux_misc.c,v 1.121 2003/06/29 22:29:30 fvdl Exp $");
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/namei.h>
72 #include <sys/proc.h>
73 #include <sys/dirent.h>
74 #include <sys/file.h>
75 #include <sys/stat.h>
76 #include <sys/filedesc.h>
77 #include <sys/ioctl.h>
78 #include <sys/kernel.h>
79 #include <sys/malloc.h>
80 #include <sys/mbuf.h>
81 #include <sys/mman.h>
82 #include <sys/mount.h>
83 #include <sys/reboot.h>
84 #include <sys/resource.h>
85 #include <sys/resourcevar.h>
86 #include <sys/signal.h>
87 #include <sys/signalvar.h>
88 #include <sys/socket.h>
89 #include <sys/time.h>
90 #include <sys/times.h>
91 #include <sys/vnode.h>
92 #include <sys/uio.h>
93 #include <sys/wait.h>
94 #include <sys/utsname.h>
95 #include <sys/unistd.h>
96 #include <sys/swap.h>		/* for SWAP_ON */
97 #include <sys/sysctl.h>		/* for KERN_DOMAINNAME */
98 
99 #include <sys/ptrace.h>
100 #include <machine/ptrace.h>
101 
102 #include <sys/sa.h>
103 #include <sys/syscallargs.h>
104 
105 #include <compat/linux/common/linux_types.h>
106 #include <compat/linux/common/linux_signal.h>
107 
108 #include <compat/linux/linux_syscallargs.h>
109 
110 #include <compat/linux/common/linux_fcntl.h>
111 #include <compat/linux/common/linux_mmap.h>
112 #include <compat/linux/common/linux_dirent.h>
113 #include <compat/linux/common/linux_util.h>
114 #include <compat/linux/common/linux_misc.h>
115 #include <compat/linux/common/linux_ptrace.h>
116 #include <compat/linux/common/linux_reboot.h>
117 #include <compat/linux/common/linux_emuldata.h>
118 
119 const int linux_ptrace_request_map[] = {
120 	LINUX_PTRACE_TRACEME,	PT_TRACE_ME,
121 	LINUX_PTRACE_PEEKTEXT,	PT_READ_I,
122 	LINUX_PTRACE_PEEKDATA,	PT_READ_D,
123 	LINUX_PTRACE_POKETEXT,	PT_WRITE_I,
124 	LINUX_PTRACE_POKEDATA,	PT_WRITE_D,
125 	LINUX_PTRACE_CONT,	PT_CONTINUE,
126 	LINUX_PTRACE_KILL,	PT_KILL,
127 	LINUX_PTRACE_ATTACH,	PT_ATTACH,
128 	LINUX_PTRACE_DETACH,	PT_DETACH,
129 #ifdef PT_STEP
130 	LINUX_PTRACE_SINGLESTEP,	PT_STEP,
131 #endif
132 	-1
133 };
134 
135 static const struct mnttypes {
136 	char *bsd;
137 	int linux;
138 } fstypes[] = {
139 	{ MOUNT_FFS,		LINUX_DEFAULT_SUPER_MAGIC	},
140 	{ MOUNT_NFS,		LINUX_NFS_SUPER_MAGIC 		},
141 	{ MOUNT_MFS,		LINUX_DEFAULT_SUPER_MAGIC	},
142 	{ MOUNT_MSDOS,		LINUX_MSDOS_SUPER_MAGIC		},
143 	{ MOUNT_LFS,		LINUX_DEFAULT_SUPER_MAGIC	},
144 	{ MOUNT_FDESC,		LINUX_DEFAULT_SUPER_MAGIC	},
145 	{ MOUNT_PORTAL,		LINUX_DEFAULT_SUPER_MAGIC	},
146 	{ MOUNT_NULL,		LINUX_DEFAULT_SUPER_MAGIC	},
147 	{ MOUNT_OVERLAY,	LINUX_DEFAULT_SUPER_MAGIC	},
148 	{ MOUNT_UMAP,		LINUX_DEFAULT_SUPER_MAGIC	},
149 	{ MOUNT_KERNFS,		LINUX_DEFAULT_SUPER_MAGIC	},
150 	{ MOUNT_PROCFS,		LINUX_PROC_SUPER_MAGIC		},
151 	{ MOUNT_AFS,		LINUX_DEFAULT_SUPER_MAGIC	},
152 	{ MOUNT_CD9660,		LINUX_ISOFS_SUPER_MAGIC		},
153 	{ MOUNT_UNION,		LINUX_DEFAULT_SUPER_MAGIC	},
154 	{ MOUNT_ADOSFS,		LINUX_ADFS_SUPER_MAGIC		},
155 	{ MOUNT_EXT2FS,		LINUX_EXT2_SUPER_MAGIC		},
156 	{ MOUNT_CFS,		LINUX_DEFAULT_SUPER_MAGIC	},
157 	{ MOUNT_CODA,		LINUX_CODA_SUPER_MAGIC		},
158 	{ MOUNT_FILECORE,	LINUX_DEFAULT_SUPER_MAGIC	},
159 	{ MOUNT_NTFS,		LINUX_DEFAULT_SUPER_MAGIC	},
160 	{ MOUNT_SMBFS,		LINUX_SMB_SUPER_MAGIC		}
161 };
162 #define FSTYPESSIZE (sizeof(fstypes) / sizeof(fstypes[0]))
163 
164 #ifdef DEBUG_LINUX
165 #define DPRINTF(a)	uprintf a
166 #else
167 #define DPRINTF(a)
168 #endif
169 
170 /* Local linux_misc.c functions: */
171 static void bsd_to_linux_statfs __P((struct statfs *, struct linux_statfs *));
172 static int linux_to_bsd_limit __P((int));
173 static void linux_to_bsd_mmap_args __P((struct sys_mmap_args *,
174     const struct linux_sys_mmap_args *));
175 
176 /*
177  * The information on a terminated (or stopped) process needs
178  * to be converted in order for Linux binaries to get a valid signal
179  * number out of it.
180  */
181 void
182 bsd_to_linux_wstat(st)
183 	int *st;
184 {
185 
186 	int sig;
187 
188 	if (WIFSIGNALED(*st)) {
189 		sig = WTERMSIG(*st);
190 		if (sig >= 0 && sig < NSIG)
191 			*st= (*st& ~0177) | native_to_linux_signo[sig];
192 	} else if (WIFSTOPPED(*st)) {
193 		sig = WSTOPSIG(*st);
194 		if (sig >= 0 && sig < NSIG)
195 			*st = (*st & ~0xff00) |
196 			    (native_to_linux_signo[sig] << 8);
197 	}
198 }
199 
200 /*
201  * This is very much the same as waitpid()
202  */
203 int
204 linux_sys_wait4(l, v, retval)
205 	struct lwp *l;
206 	void *v;
207 	register_t *retval;
208 {
209 	struct linux_sys_wait4_args /* {
210 		syscallarg(int) pid;
211 		syscallarg(int *) status;
212 		syscallarg(int) options;
213 		syscallarg(struct rusage *) rusage;
214 	} */ *uap = v;
215 	struct proc *p = l->l_proc;
216 	struct sys_wait4_args w4a;
217 	int error, *status, tstat, options, linux_options;
218 	caddr_t sg;
219 
220 	if (SCARG(uap, status) != NULL) {
221 		sg = stackgap_init(p, 0);
222 		status = (int *) stackgap_alloc(p, &sg, sizeof *status);
223 	} else
224 		status = NULL;
225 
226 	linux_options = SCARG(uap, options);
227 	options = 0;
228 	if (linux_options &
229 	    ~(LINUX_WAIT4_WNOHANG|LINUX_WAIT4_WUNTRACED|LINUX_WAIT4_WALL|
230 	      LINUX_WAIT4_WCLONE))
231 		return (EINVAL);
232 
233 	if (linux_options & LINUX_WAIT4_WNOHANG)
234 		options |= WNOHANG;
235 	if (linux_options & LINUX_WAIT4_WUNTRACED)
236 		options |= WUNTRACED;
237 	if (linux_options & LINUX_WAIT4_WALL)
238 		options |= WALLSIG;
239 	if (linux_options & LINUX_WAIT4_WCLONE)
240 		options |= WALTSIG;
241 
242 	SCARG(&w4a, pid) = SCARG(uap, pid);
243 	SCARG(&w4a, status) = status;
244 	SCARG(&w4a, options) = options;
245 	SCARG(&w4a, rusage) = SCARG(uap, rusage);
246 
247 	if ((error = sys_wait4(l, &w4a, retval)))
248 		return error;
249 
250 	sigdelset(&p->p_sigctx.ps_siglist, SIGCHLD);
251 
252 	if (status != NULL) {
253 		if ((error = copyin(status, &tstat, sizeof tstat)))
254 			return error;
255 
256 		bsd_to_linux_wstat(&tstat);
257 		return copyout(&tstat, SCARG(uap, status), sizeof tstat);
258 	}
259 
260 	return 0;
261 }
262 
263 /*
264  * Linux brk(2). The check if the new address is >= the old one is
265  * done in the kernel in Linux. NetBSD does it in the library.
266  */
267 int
268 linux_sys_brk(l, v, retval)
269 	struct lwp *l;
270 	void *v;
271 	register_t *retval;
272 {
273 	struct linux_sys_brk_args /* {
274 		syscallarg(char *) nsize;
275 	} */ *uap = v;
276 	struct proc *p = l->l_proc;
277 	char *nbrk = SCARG(uap, nsize);
278 	struct sys_obreak_args oba;
279 	struct vmspace *vm = p->p_vmspace;
280 	struct linux_emuldata *ed = (struct linux_emuldata*)p->p_emuldata;
281 
282 	SCARG(&oba, nsize) = nbrk;
283 
284 	if ((caddr_t) nbrk > vm->vm_daddr && sys_obreak(l, &oba, retval) == 0)
285 		ed->p_break = (char*)nbrk;
286 	else
287 		nbrk = ed->p_break;
288 
289 	retval[0] = (register_t)nbrk;
290 
291 	return 0;
292 }
293 
294 /*
295  * Convert BSD statfs structure to Linux statfs structure.
296  * The Linux structure has less fields, and it also wants
297  * the length of a name in a dir entry in a field, which
298  * we fake (probably the wrong way).
299  */
300 static void
301 bsd_to_linux_statfs(bsp, lsp)
302 	struct statfs *bsp;
303 	struct linux_statfs *lsp;
304 {
305 	int i;
306 
307 	for (i = 0; i < FSTYPESSIZE; i++)
308 		if (strcmp(bsp->f_fstypename, fstypes[i].bsd) == 0)
309 			break;
310 
311 	if (i == FSTYPESSIZE) {
312 		DPRINTF(("unhandled fstype in linux emulation: %s\n",
313 		    bsp->f_fstypename));
314 		lsp->l_ftype = LINUX_DEFAULT_SUPER_MAGIC;
315 	} else {
316 		lsp->l_ftype = fstypes[i].linux;
317 	}
318 
319 	lsp->l_fbsize = bsp->f_bsize;
320 	lsp->l_fblocks = bsp->f_blocks;
321 	lsp->l_fbfree = bsp->f_bfree;
322 	lsp->l_fbavail = bsp->f_bavail;
323 	lsp->l_ffiles = bsp->f_files;
324 	lsp->l_fffree = bsp->f_ffree;
325 	/* Linux sets the fsid to 0..., we don't */
326 	lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
327 	lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
328 	lsp->l_fnamelen = MAXNAMLEN;	/* XXX */
329 	(void)memset(lsp->l_fspare, 0, sizeof(lsp->l_fspare));
330 }
331 
332 /*
333  * Implement the fs stat functions. Straightforward.
334  */
335 int
336 linux_sys_statfs(l, v, retval)
337 	struct lwp *l;
338 	void *v;
339 	register_t *retval;
340 {
341 	struct linux_sys_statfs_args /* {
342 		syscallarg(const char *) path;
343 		syscallarg(struct linux_statfs *) sp;
344 	} */ *uap = v;
345 	struct proc *p = l->l_proc;
346 	struct statfs btmp, *bsp;
347 	struct linux_statfs ltmp;
348 	struct sys_statfs_args bsa;
349 	caddr_t sg;
350 	int error;
351 
352 	sg = stackgap_init(p, 0);
353 	bsp = (struct statfs *) stackgap_alloc(p, &sg, sizeof (struct statfs));
354 
355 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
356 
357 	SCARG(&bsa, path) = SCARG(uap, path);
358 	SCARG(&bsa, buf) = bsp;
359 
360 	if ((error = sys_statfs(l, &bsa, retval)))
361 		return error;
362 
363 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
364 		return error;
365 
366 	bsd_to_linux_statfs(&btmp, &ltmp);
367 
368 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
369 }
370 
371 int
372 linux_sys_fstatfs(l, v, retval)
373 	struct lwp *l;
374 	void *v;
375 	register_t *retval;
376 {
377 	struct linux_sys_fstatfs_args /* {
378 		syscallarg(int) fd;
379 		syscallarg(struct linux_statfs *) sp;
380 	} */ *uap = v;
381 	struct proc *p = l->l_proc;
382 	struct statfs btmp, *bsp;
383 	struct linux_statfs ltmp;
384 	struct sys_fstatfs_args bsa;
385 	caddr_t sg;
386 	int error;
387 
388 	sg = stackgap_init(p, 0);
389 	bsp = (struct statfs *) stackgap_alloc(p, &sg, sizeof (struct statfs));
390 
391 	SCARG(&bsa, fd) = SCARG(uap, fd);
392 	SCARG(&bsa, buf) = bsp;
393 
394 	if ((error = sys_fstatfs(l, &bsa, retval)))
395 		return error;
396 
397 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
398 		return error;
399 
400 	bsd_to_linux_statfs(&btmp, &ltmp);
401 
402 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
403 }
404 
405 /*
406  * uname(). Just copy the info from the various strings stored in the
407  * kernel, and put it in the Linux utsname structure. That structure
408  * is almost the same as the NetBSD one, only it has fields 65 characters
409  * long, and an extra domainname field.
410  */
411 int
412 linux_sys_uname(l, v, retval)
413 	struct lwp *l;
414 	void *v;
415 	register_t *retval;
416 {
417 	struct linux_sys_uname_args /* {
418 		syscallarg(struct linux_utsname *) up;
419 	} */ *uap = v;
420 	struct linux_utsname luts;
421 
422 	strncpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
423 	strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
424 	strncpy(luts.l_release, linux_release, sizeof(luts.l_release));
425 	strncpy(luts.l_version, linux_version, sizeof(luts.l_version));
426 	strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
427 	strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
428 
429 	return copyout(&luts, SCARG(uap, up), sizeof(luts));
430 }
431 
432 /* Used directly on: alpha, mips, ppc, sparc, sparc64 */
433 /* Used indirectly on: arm, i386, m68k */
434 
435 /*
436  * New type Linux mmap call.
437  * Only called directly on machines with >= 6 free regs.
438  */
439 int
440 linux_sys_mmap(l, v, retval)
441 	struct lwp *l;
442 	void *v;
443 	register_t *retval;
444 {
445 	struct linux_sys_mmap_args /* {
446 		syscallarg(unsigned long) addr;
447 		syscallarg(size_t) len;
448 		syscallarg(int) prot;
449 		syscallarg(int) flags;
450 		syscallarg(int) fd;
451 		syscallarg(linux_off_t) offset;
452 	} */ *uap = v;
453 	struct sys_mmap_args cma;
454 
455 	if (SCARG(uap, offset) & PAGE_MASK)
456 		return EINVAL;
457 
458 	linux_to_bsd_mmap_args(&cma, uap);
459 	SCARG(&cma, pos) = (off_t)SCARG(uap, offset);
460 
461 	return sys_mmap(l, &cma, retval);
462 }
463 
464 /*
465  * Guts of most architectures' mmap64() implementations.  This shares
466  * its list of arguments with linux_sys_mmap().
467  *
468  * The difference in linux_sys_mmap2() is that "offset" is actually
469  * (offset / pagesize), not an absolute byte count.  This translation
470  * to pagesize offsets is done inside glibc between the mmap64() call
471  * point, and the actual syscall.
472  */
473 int
474 linux_sys_mmap2(l, v, retval)
475 	struct lwp *l;
476 	void *v;
477 	register_t *retval;
478 {
479 	struct linux_sys_mmap2_args /* {
480 		syscallarg(unsigned long) addr;
481 		syscallarg(size_t) len;
482 		syscallarg(int) prot;
483 		syscallarg(int) flags;
484 		syscallarg(int) fd;
485 		syscallarg(linux_off_t) offset;
486 	} */ *uap = v;
487 	struct sys_mmap_args cma;
488 
489 	linux_to_bsd_mmap_args(&cma, uap);
490 	SCARG(&cma, pos) = ((off_t)SCARG(uap, offset)) << PAGE_SHIFT;
491 
492 	return sys_mmap(l, &cma, retval);
493 }
494 
495 static void
496 linux_to_bsd_mmap_args(cma, uap)
497 	struct sys_mmap_args *cma;
498 	const struct linux_sys_mmap_args *uap;
499 {
500 	int flags = MAP_TRYFIXED, fl = SCARG(uap, flags);
501 
502 	flags |= cvtto_bsd_mask(fl, LINUX_MAP_SHARED, MAP_SHARED);
503 	flags |= cvtto_bsd_mask(fl, LINUX_MAP_PRIVATE, MAP_PRIVATE);
504 	flags |= cvtto_bsd_mask(fl, LINUX_MAP_FIXED, MAP_FIXED);
505 	flags |= cvtto_bsd_mask(fl, LINUX_MAP_ANON, MAP_ANON);
506 	/* XXX XAX ERH: Any other flags here?  There are more defined... */
507 
508 	SCARG(cma, addr) = (void *)SCARG(uap, addr);
509 	SCARG(cma, len) = SCARG(uap, len);
510 	SCARG(cma, prot) = SCARG(uap, prot);
511 	if (SCARG(cma, prot) & VM_PROT_WRITE) /* XXX */
512 		SCARG(cma, prot) |= VM_PROT_READ;
513 	SCARG(cma, flags) = flags;
514 	SCARG(cma, fd) = flags & MAP_ANON ? -1 : SCARG(uap, fd);
515 	SCARG(cma, pad) = 0;
516 }
517 
518 int
519 linux_sys_mremap(l, v, retval)
520 	struct lwp *l;
521 	void *v;
522 	register_t *retval;
523 {
524 	struct linux_sys_mremap_args /* {
525 		syscallarg(void *) old_address;
526 		syscallarg(size_t) old_size;
527 		syscallarg(size_t) new_size;
528 		syscallarg(u_long) flags;
529 	} */ *uap = v;
530 	struct sys_munmap_args mua;
531 	size_t old_size, new_size;
532 	int error;
533 
534 	old_size = round_page(SCARG(uap, old_size));
535 	new_size = round_page(SCARG(uap, new_size));
536 
537 	/*
538 	 * Growing mapped region.
539 	 */
540 	if (new_size > old_size) {
541 		/*
542 		 * XXX Implement me.  What we probably want to do is
543 		 * XXX dig out the guts of the old mapping, mmap that
544 		 * XXX object again with the new size, then munmap
545 		 * XXX the old mapping.
546 		 */
547 		*retval = 0;
548 		return (ENOMEM);
549 	}
550 
551 	/*
552 	 * Shrinking mapped region.
553 	 */
554 	if (new_size < old_size) {
555 		SCARG(&mua, addr) = (caddr_t)SCARG(uap, old_address) +
556 		    new_size;
557 		SCARG(&mua, len) = old_size - new_size;
558 		error = sys_munmap(l, &mua, retval);
559 		*retval = error ? 0 : (register_t)SCARG(uap, old_address);
560 		return (error);
561 	}
562 
563 	/*
564 	 * No change.
565 	 */
566 	*retval = (register_t)SCARG(uap, old_address);
567 	return (0);
568 }
569 
570 int
571 linux_sys_msync(l, v, retval)
572 	struct lwp *l;
573 	void *v;
574 	register_t *retval;
575 {
576 	struct linux_sys_msync_args /* {
577 		syscallarg(caddr_t) addr;
578 		syscallarg(int) len;
579 		syscallarg(int) fl;
580 	} */ *uap = v;
581 
582 	struct sys___msync13_args bma;
583 
584 	/* flags are ignored */
585 	SCARG(&bma, addr) = SCARG(uap, addr);
586 	SCARG(&bma, len) = SCARG(uap, len);
587 	SCARG(&bma, flags) = SCARG(uap, fl);
588 
589 	return sys___msync13(l, &bma, retval);
590 }
591 
592 int
593 linux_sys_mprotect(l, v, retval)
594 	struct lwp *l;
595 	void *v;
596 	register_t *retval;
597 {
598 	struct linux_sys_mprotect_args /* {
599 		syscallarg(const void *) start;
600 		syscallarg(unsigned long) len;
601 		syscallarg(int) prot;
602 	} */ *uap = v;
603 	unsigned long end, start = (unsigned long)SCARG(uap, start), len;
604 	int prot = SCARG(uap, prot);
605 	struct vm_map_entry *entry;
606 	struct vm_map *map = &l->l_proc->p_vmspace->vm_map;
607 
608 	if (start & PAGE_MASK)
609 		return EINVAL;
610 
611 	len = round_page(SCARG(uap, len));
612 	end = start + len;
613 
614 	if (end < start)
615 		return EINVAL;
616 	else if (end == start)
617 		return 0;
618 
619 	if (SCARG(uap, prot) & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
620 		return EINVAL;
621 
622 	vm_map_lock(map);
623 #ifdef notdef
624 	VM_MAP_RANGE_CHECK(map, start, end);
625 #endif
626 	if (!uvm_map_lookup_entry(map, start, &entry) || entry->start > start) {
627 		vm_map_unlock(map);
628 		return EFAULT;
629 	}
630 	vm_map_unlock(map);
631 	return uvm_map_protect(map, start, end, prot, FALSE);
632 }
633 
634 /*
635  * This code is partly stolen from src/lib/libc/compat-43/times.c
636  */
637 
638 #define	CONVTCK(r)	(r.tv_sec * hz + r.tv_usec / (1000000 / hz))
639 
640 int
641 linux_sys_times(l, v, retval)
642 	struct lwp *l;
643 	void *v;
644 	register_t *retval;
645 {
646 	struct linux_sys_times_args /* {
647 		syscallarg(struct times *) tms;
648 	} */ *uap = v;
649 	struct proc *p = l->l_proc;
650 	struct timeval t;
651 	int error, s;
652 
653 	if (SCARG(uap, tms)) {
654 		struct linux_tms ltms;
655 		struct rusage ru;
656 
657 		calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
658 		ltms.ltms_utime = CONVTCK(ru.ru_utime);
659 		ltms.ltms_stime = CONVTCK(ru.ru_stime);
660 
661 		ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
662 		ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
663 
664 		if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
665 			return error;
666 	}
667 
668 	s = splclock();
669 	timersub(&time, &boottime, &t);
670 	splx(s);
671 
672 	retval[0] = ((linux_clock_t)(CONVTCK(t)));
673 	return 0;
674 }
675 
676 #undef CONVTCK
677 
678 /*
679  * Linux 'readdir' call. This code is mostly taken from the
680  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
681  * an attempt has been made to keep it a little cleaner (failing
682  * miserably, because of the cruft needed if count 1 is passed).
683  *
684  * The d_off field should contain the offset of the next valid entry,
685  * but in Linux it has the offset of the entry itself. We emulate
686  * that bug here.
687  *
688  * Read in BSD-style entries, convert them, and copy them out.
689  *
690  * Note that this doesn't handle union-mounted filesystems.
691  */
692 int
693 linux_sys_getdents(l, v, retval)
694 	struct lwp *l;
695 	void *v;
696 	register_t *retval;
697 {
698 	struct linux_sys_getdents_args /* {
699 		syscallarg(int) fd;
700 		syscallarg(struct linux_dirent *) dent;
701 		syscallarg(unsigned int) count;
702 	} */ *uap = v;
703 	struct proc *p = l->l_proc;
704 	struct dirent *bdp;
705 	struct vnode *vp;
706 	caddr_t	inp, buf;		/* BSD-format */
707 	int len, reclen;		/* BSD-format */
708 	caddr_t outp;			/* Linux-format */
709 	int resid, linux_reclen = 0;	/* Linux-format */
710 	struct file *fp;
711 	struct uio auio;
712 	struct iovec aiov;
713 	struct linux_dirent idb;
714 	off_t off;		/* true file offset */
715 	int buflen, error, eofflag, nbytes, oldcall;
716 	struct vattr va;
717 	off_t *cookiebuf = NULL, *cookie;
718 	int ncookies;
719 
720 	/* getvnode() will use the descriptor for us */
721 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
722 		return (error);
723 
724 	if ((fp->f_flag & FREAD) == 0) {
725 		error = EBADF;
726 		goto out1;
727 	}
728 
729 	vp = (struct vnode *)fp->f_data;
730 	if (vp->v_type != VDIR) {
731 		error = EINVAL;
732 		goto out1;
733 	}
734 
735 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
736 		goto out1;
737 
738 	nbytes = SCARG(uap, count);
739 	if (nbytes == 1) {	/* emulating old, broken behaviour */
740 		nbytes = sizeof (idb);
741 		buflen = max(va.va_blocksize, nbytes);
742 		oldcall = 1;
743 	} else {
744 		buflen = min(MAXBSIZE, nbytes);
745 		if (buflen < va.va_blocksize)
746 			buflen = va.va_blocksize;
747 		oldcall = 0;
748 	}
749 	buf = malloc(buflen, M_TEMP, M_WAITOK);
750 
751 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
752 	off = fp->f_offset;
753 again:
754 	aiov.iov_base = buf;
755 	aiov.iov_len = buflen;
756 	auio.uio_iov = &aiov;
757 	auio.uio_iovcnt = 1;
758 	auio.uio_rw = UIO_READ;
759 	auio.uio_segflg = UIO_SYSSPACE;
760 	auio.uio_procp = p;
761 	auio.uio_resid = buflen;
762 	auio.uio_offset = off;
763 	/*
764          * First we read into the malloc'ed buffer, then
765          * we massage it into user space, one record at a time.
766          */
767 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
768 	    &ncookies);
769 	if (error)
770 		goto out;
771 
772 	inp = buf;
773 	outp = (caddr_t)SCARG(uap, dent);
774 	resid = nbytes;
775 	if ((len = buflen - auio.uio_resid) == 0)
776 		goto eof;
777 
778 	for (cookie = cookiebuf; len > 0; len -= reclen) {
779 		bdp = (struct dirent *)inp;
780 		reclen = bdp->d_reclen;
781 		if (reclen & 3)
782 			panic("linux_readdir");
783 		if (bdp->d_fileno == 0) {
784 			inp += reclen;	/* it is a hole; squish it out */
785 			off = *cookie++;
786 			continue;
787 		}
788 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
789 		if (reclen > len || resid < linux_reclen) {
790 			/* entry too big for buffer, so just stop */
791 			outp++;
792 			break;
793 		}
794 		/*
795 		 * Massage in place to make a Linux-shaped dirent (otherwise
796 		 * we have to worry about touching user memory outside of
797 		 * the copyout() call).
798 		 */
799 		idb.d_ino = bdp->d_fileno;
800 		/*
801 		 * The old readdir() call misuses the offset and reclen fields.
802 		 */
803 		if (oldcall) {
804 			idb.d_off = (linux_off_t)linux_reclen;
805 			idb.d_reclen = (u_short)bdp->d_namlen;
806 		} else {
807 			if (sizeof (idb.d_off) <= 4 && (off >> 32) != 0) {
808 				compat_offseterr(vp, "linux_getdents");
809 				error = EINVAL;
810 				goto out;
811 			}
812 			idb.d_off = (linux_off_t)off;
813 			idb.d_reclen = (u_short)linux_reclen;
814 		}
815 		strcpy(idb.d_name, bdp->d_name);
816 		if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
817 			goto out;
818 		/* advance past this real entry */
819 		inp += reclen;
820 		off = *cookie++;	/* each entry points to itself */
821 		/* advance output past Linux-shaped entry */
822 		outp += linux_reclen;
823 		resid -= linux_reclen;
824 		if (oldcall)
825 			break;
826 	}
827 
828 	/* if we squished out the whole block, try again */
829 	if (outp == (caddr_t)SCARG(uap, dent))
830 		goto again;
831 	fp->f_offset = off;	/* update the vnode offset */
832 
833 	if (oldcall)
834 		nbytes = resid + linux_reclen;
835 
836 eof:
837 	*retval = nbytes - resid;
838 out:
839 	VOP_UNLOCK(vp, 0);
840 	if (cookiebuf)
841 		free(cookiebuf, M_TEMP);
842 	free(buf, M_TEMP);
843 out1:
844 	FILE_UNUSE(fp, p);
845 	return error;
846 }
847 
848 /*
849  * Even when just using registers to pass arguments to syscalls you can
850  * have 5 of them on the i386. So this newer version of select() does
851  * this.
852  */
853 int
854 linux_sys_select(l, v, retval)
855 	struct lwp *l;
856 	void *v;
857 	register_t *retval;
858 {
859 	struct linux_sys_select_args /* {
860 		syscallarg(int) nfds;
861 		syscallarg(fd_set *) readfds;
862 		syscallarg(fd_set *) writefds;
863 		syscallarg(fd_set *) exceptfds;
864 		syscallarg(struct timeval *) timeout;
865 	} */ *uap = v;
866 
867 	return linux_select1(l, retval, SCARG(uap, nfds), SCARG(uap, readfds),
868 	    SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
869 }
870 
871 /*
872  * Common code for the old and new versions of select(). A couple of
873  * things are important:
874  * 1) return the amount of time left in the 'timeout' parameter
875  * 2) select never returns ERESTART on Linux, always return EINTR
876  */
877 int
878 linux_select1(l, retval, nfds, readfds, writefds, exceptfds, timeout)
879 	struct lwp *l;
880 	register_t *retval;
881 	int nfds;
882 	fd_set *readfds, *writefds, *exceptfds;
883 	struct timeval *timeout;
884 {
885 	struct sys_select_args bsa;
886 	struct proc *p = l->l_proc;
887 	struct timeval tv0, tv1, utv, *tvp;
888 	caddr_t sg;
889 	int error;
890 
891 	SCARG(&bsa, nd) = nfds;
892 	SCARG(&bsa, in) = readfds;
893 	SCARG(&bsa, ou) = writefds;
894 	SCARG(&bsa, ex) = exceptfds;
895 	SCARG(&bsa, tv) = timeout;
896 
897 	/*
898 	 * Store current time for computation of the amount of
899 	 * time left.
900 	 */
901 	if (timeout) {
902 		if ((error = copyin(timeout, &utv, sizeof(utv))))
903 			return error;
904 		if (itimerfix(&utv)) {
905 			/*
906 			 * The timeval was invalid.  Convert it to something
907 			 * valid that will act as it does under Linux.
908 			 */
909 			sg = stackgap_init(p, 0);
910 			tvp = stackgap_alloc(p, &sg, sizeof(utv));
911 			utv.tv_sec += utv.tv_usec / 1000000;
912 			utv.tv_usec %= 1000000;
913 			if (utv.tv_usec < 0) {
914 				utv.tv_sec -= 1;
915 				utv.tv_usec += 1000000;
916 			}
917 			if (utv.tv_sec < 0)
918 				timerclear(&utv);
919 			if ((error = copyout(&utv, tvp, sizeof(utv))))
920 				return error;
921 			SCARG(&bsa, tv) = tvp;
922 		}
923 		microtime(&tv0);
924 	}
925 
926 	error = sys_select(l, &bsa, retval);
927 	if (error) {
928 		/*
929 		 * See fs/select.c in the Linux kernel.  Without this,
930 		 * Maelstrom doesn't work.
931 		 */
932 		if (error == ERESTART)
933 			error = EINTR;
934 		return error;
935 	}
936 
937 	if (timeout) {
938 		if (*retval) {
939 			/*
940 			 * Compute how much time was left of the timeout,
941 			 * by subtracting the current time and the time
942 			 * before we started the call, and subtracting
943 			 * that result from the user-supplied value.
944 			 */
945 			microtime(&tv1);
946 			timersub(&tv1, &tv0, &tv1);
947 			timersub(&utv, &tv1, &utv);
948 			if (utv.tv_sec < 0)
949 				timerclear(&utv);
950 		} else
951 			timerclear(&utv);
952 		if ((error = copyout(&utv, timeout, sizeof(utv))))
953 			return error;
954 	}
955 
956 	return 0;
957 }
958 
959 /*
960  * Get the process group of a certain process. Look it up
961  * and return the value.
962  */
963 int
964 linux_sys_getpgid(l, v, retval)
965 	struct lwp *l;
966 	void *v;
967 	register_t *retval;
968 {
969 	struct linux_sys_getpgid_args /* {
970 		syscallarg(int) pid;
971 	} */ *uap = v;
972 	struct proc *p = l->l_proc;
973 	struct proc *targp;
974 
975 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
976 		if ((targp = pfind(SCARG(uap, pid))) == 0)
977 			return ESRCH;
978 	}
979 	else
980 		targp = p;
981 
982 	retval[0] = targp->p_pgid;
983 	return 0;
984 }
985 
986 /*
987  * Set the 'personality' (emulation mode) for the current process. Only
988  * accept the Linux personality here (0). This call is needed because
989  * the Linux ELF crt0 issues it in an ugly kludge to make sure that
990  * ELF binaries run in Linux mode, not SVR4 mode.
991  */
992 int
993 linux_sys_personality(l, v, retval)
994 	struct lwp *l;
995 	void *v;
996 	register_t *retval;
997 {
998 	struct linux_sys_personality_args /* {
999 		syscallarg(int) per;
1000 	} */ *uap = v;
1001 
1002 	if (SCARG(uap, per) != 0)
1003 		return EINVAL;
1004 	retval[0] = 0;
1005 	return 0;
1006 }
1007 
1008 #if defined(__i386__) || defined(__m68k__)
1009 /*
1010  * The calls are here because of type conversions.
1011  */
1012 int
1013 linux_sys_setreuid16(l, v, retval)
1014 	struct lwp *l;
1015 	void *v;
1016 	register_t *retval;
1017 {
1018 	struct linux_sys_setreuid16_args /* {
1019 		syscallarg(int) ruid;
1020 		syscallarg(int) euid;
1021 	} */ *uap = v;
1022 	struct sys_setreuid_args bsa;
1023 
1024 	SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
1025 		(uid_t)-1 : SCARG(uap, ruid);
1026 	SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
1027 		(uid_t)-1 : SCARG(uap, euid);
1028 
1029 	return sys_setreuid(l, &bsa, retval);
1030 }
1031 
1032 int
1033 linux_sys_setregid16(l, v, retval)
1034 	struct lwp *l;
1035 	void *v;
1036 	register_t *retval;
1037 {
1038 	struct linux_sys_setregid16_args /* {
1039 		syscallarg(int) rgid;
1040 		syscallarg(int) egid;
1041 	} */ *uap = v;
1042 	struct sys_setregid_args bsa;
1043 
1044 	SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
1045 		(uid_t)-1 : SCARG(uap, rgid);
1046 	SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
1047 		(uid_t)-1 : SCARG(uap, egid);
1048 
1049 	return sys_setregid(l, &bsa, retval);
1050 }
1051 
1052 int
1053 linux_sys_setresuid16(l, v, retval)
1054 	struct lwp *l;
1055 	void *v;
1056 	register_t *retval;
1057 {
1058 	struct linux_sys_setresuid16_args /* {
1059 		syscallarg(uid_t) ruid;
1060 		syscallarg(uid_t) euid;
1061 		syscallarg(uid_t) suid;
1062 	} */ *uap = v;
1063 	struct linux_sys_setresuid16_args lsa;
1064 
1065 	SCARG(&lsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
1066 		(uid_t)-1 : SCARG(uap, ruid);
1067 	SCARG(&lsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
1068 		(uid_t)-1 : SCARG(uap, euid);
1069 	SCARG(&lsa, suid) = ((linux_uid_t)SCARG(uap, suid) == (linux_uid_t)-1) ?
1070 		(uid_t)-1 : SCARG(uap, suid);
1071 
1072 	return linux_sys_setresuid(l, &lsa, retval);
1073 }
1074 
1075 int
1076 linux_sys_setresgid16(l, v, retval)
1077 	struct lwp *l;
1078 	void *v;
1079 	register_t *retval;
1080 {
1081 	struct linux_sys_setresgid16_args /* {
1082 		syscallarg(gid_t) rgid;
1083 		syscallarg(gid_t) egid;
1084 		syscallarg(gid_t) sgid;
1085 	} */ *uap = v;
1086 	struct linux_sys_setresgid16_args lsa;
1087 
1088 	SCARG(&lsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
1089 		(gid_t)-1 : SCARG(uap, rgid);
1090 	SCARG(&lsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
1091 		(gid_t)-1 : SCARG(uap, egid);
1092 	SCARG(&lsa, sgid) = ((linux_gid_t)SCARG(uap, sgid) == (linux_gid_t)-1) ?
1093 		(gid_t)-1 : SCARG(uap, sgid);
1094 
1095 	return linux_sys_setresgid(l, &lsa, retval);
1096 }
1097 
1098 int
1099 linux_sys_getgroups16(l, v, retval)
1100 	struct lwp *l;
1101 	void *v;
1102 	register_t *retval;
1103 {
1104 	struct linux_sys_getgroups16_args /* {
1105 		syscallarg(int) gidsetsize;
1106 		syscallarg(linux_gid_t *) gidset;
1107 	} */ *uap = v;
1108 	struct proc *p = l->l_proc;
1109 	caddr_t sg;
1110 	int n, error, i;
1111 	struct sys_getgroups_args bsa;
1112 	gid_t *bset, *kbset;
1113 	linux_gid_t *lset;
1114 	struct pcred *pc = p->p_cred;
1115 
1116 	n = SCARG(uap, gidsetsize);
1117 	if (n < 0)
1118 		return EINVAL;
1119 	error = 0;
1120 	bset = kbset = NULL;
1121 	lset = NULL;
1122 	if (n > 0) {
1123 		n = min(pc->pc_ucred->cr_ngroups, n);
1124 		sg = stackgap_init(p, 0);
1125 		bset = stackgap_alloc(p, &sg, n * sizeof (gid_t));
1126 		kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
1127 		lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
1128 		if (bset == NULL || kbset == NULL || lset == NULL)
1129 			return ENOMEM;
1130 		SCARG(&bsa, gidsetsize) = n;
1131 		SCARG(&bsa, gidset) = bset;
1132 		error = sys_getgroups(l, &bsa, retval);
1133 		if (error != 0)
1134 			goto out;
1135 		error = copyin(bset, kbset, n * sizeof (gid_t));
1136 		if (error != 0)
1137 			goto out;
1138 		for (i = 0; i < n; i++)
1139 			lset[i] = (linux_gid_t)kbset[i];
1140 		error = copyout(lset, SCARG(uap, gidset),
1141 		    n * sizeof (linux_gid_t));
1142 	} else
1143 		*retval = pc->pc_ucred->cr_ngroups;
1144 out:
1145 	if (kbset != NULL)
1146 		free(kbset, M_TEMP);
1147 	if (lset != NULL)
1148 		free(lset, M_TEMP);
1149 	return error;
1150 }
1151 
1152 int
1153 linux_sys_setgroups16(l, v, retval)
1154 	struct lwp *l;
1155 	void *v;
1156 	register_t *retval;
1157 {
1158 	struct linux_sys_setgroups16_args /* {
1159 		syscallarg(int) gidsetsize;
1160 		syscallarg(linux_gid_t *) gidset;
1161 	} */ *uap = v;
1162 	struct proc *p = l->l_proc;
1163 	caddr_t sg;
1164 	int n;
1165 	int error, i;
1166 	struct sys_setgroups_args bsa;
1167 	gid_t *bset, *kbset;
1168 	linux_gid_t *lset;
1169 
1170 	n = SCARG(uap, gidsetsize);
1171 	if (n < 0 || n > NGROUPS)
1172 		return EINVAL;
1173 	sg = stackgap_init(p, 0);
1174 	bset = stackgap_alloc(p, &sg, n * sizeof (gid_t));
1175 	lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
1176 	kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
1177 	if (lset == NULL || bset == NULL)
1178 		return ENOMEM;
1179 	error = copyin(SCARG(uap, gidset), lset, n * sizeof (linux_gid_t));
1180 	if (error != 0)
1181 		goto out;
1182 	for (i = 0; i < n; i++)
1183 		kbset[i] = (gid_t)lset[i];
1184 	error = copyout(kbset, bset, n * sizeof (gid_t));
1185 	if (error != 0)
1186 		goto out;
1187 	SCARG(&bsa, gidsetsize) = n;
1188 	SCARG(&bsa, gidset) = bset;
1189 	error = sys_setgroups(l, &bsa, retval);
1190 
1191 out:
1192 	if (lset != NULL)
1193 		free(lset, M_TEMP);
1194 	if (kbset != NULL)
1195 		free(kbset, M_TEMP);
1196 
1197 	return error;
1198 }
1199 
1200 #endif /* __i386__ || __m68k__ */
1201 
1202 /*
1203  * We have nonexistent fsuid equal to uid.
1204  * If modification is requested, refuse.
1205  */
1206 int
1207 linux_sys_setfsuid(l, v, retval)
1208 	 struct lwp *l;
1209 	 void *v;
1210 	 register_t *retval;
1211 {
1212 	 struct linux_sys_setfsuid_args /* {
1213 		 syscallarg(uid_t) uid;
1214 	 } */ *uap = v;
1215 	 struct proc *p = l->l_proc;
1216 	 uid_t uid;
1217 
1218 	 uid = SCARG(uap, uid);
1219 	 if (p->p_cred->p_ruid != uid)
1220 		 return sys_nosys(l, v, retval);
1221 	 else
1222 		 return (0);
1223 }
1224 
1225 /* XXX XXX XXX */
1226 #ifndef alpha
1227 int
1228 linux_sys_getfsuid(l, v, retval)
1229 	struct lwp *l;
1230 	void *v;
1231 	register_t *retval;
1232 {
1233 	return sys_getuid(l, v, retval);
1234 }
1235 #endif
1236 
1237 int
1238 linux_sys_setresuid(l, v, retval)
1239 	struct lwp *l;
1240 	void *v;
1241 	register_t *retval;
1242 {
1243 	struct linux_sys_setresuid_args /* {
1244 		syscallarg(uid_t) ruid;
1245 		syscallarg(uid_t) euid;
1246 		syscallarg(uid_t) suid;
1247 	} */ *uap = v;
1248 
1249 	/*
1250 	 * Note: These checks are a little different than the NetBSD
1251 	 * setreuid(2) call performs.  This precisely follows the
1252 	 * behavior of the Linux kernel.
1253 	 */
1254 
1255 	return do_setresuid(l, SCARG(uap, ruid), SCARG(uap, euid),
1256 			    SCARG(uap, suid),
1257 			    ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S |
1258 			    ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
1259 			    ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S );
1260 }
1261 
1262 int
1263 linux_sys_getresuid(l, v, retval)
1264 	struct lwp *l;
1265 	void *v;
1266 	register_t *retval;
1267 {
1268 	struct linux_sys_getresuid_args /* {
1269 		syscallarg(uid_t *) ruid;
1270 		syscallarg(uid_t *) euid;
1271 		syscallarg(uid_t *) suid;
1272 	} */ *uap = v;
1273 	struct proc *p = l->l_proc;
1274 	struct pcred *pc = p->p_cred;
1275 	int error;
1276 
1277 	/*
1278 	 * Linux copies these values out to userspace like so:
1279 	 *
1280 	 *	1. Copy out ruid.
1281 	 *	2. If that succeeds, copy out euid.
1282 	 *	3. If both of those succeed, copy out suid.
1283 	 */
1284 	if ((error = copyout(&pc->p_ruid, SCARG(uap, ruid),
1285 			     sizeof(uid_t))) != 0)
1286 		return (error);
1287 
1288 	if ((error = copyout(&pc->pc_ucred->cr_uid, SCARG(uap, euid),
1289 			     sizeof(uid_t))) != 0)
1290 		return (error);
1291 
1292 	return (copyout(&pc->p_svuid, SCARG(uap, suid), sizeof(uid_t)));
1293 }
1294 
1295 int
1296 linux_sys_ptrace(l, v, retval)
1297 	struct lwp *l;
1298 	void *v;
1299 	register_t *retval;
1300 {
1301 	struct linux_sys_ptrace_args /* {
1302 		i386, m68k, powerpc: T=int
1303 		alpha: T=long
1304 		syscallarg(T) request;
1305 		syscallarg(T) pid;
1306 		syscallarg(T) addr;
1307 		syscallarg(T) data;
1308 	} */ *uap = v;
1309 	const int *ptr;
1310 	int request;
1311 	int error;
1312 
1313 	ptr = linux_ptrace_request_map;
1314 	request = SCARG(uap, request);
1315 	while (*ptr != -1)
1316 		if (*ptr++ == request) {
1317 			struct sys_ptrace_args pta;
1318 
1319 			SCARG(&pta, req) = *ptr;
1320 			SCARG(&pta, pid) = SCARG(uap, pid);
1321 			SCARG(&pta, addr) = (caddr_t)SCARG(uap, addr);
1322 			SCARG(&pta, data) = SCARG(uap, data);
1323 
1324 			/*
1325 			 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
1326 			 * to continue where the process left off previously.
1327 			 * The same thing is achieved by addr == (caddr_t) 1
1328 			 * on NetBSD, so rewrite 'addr' appropriately.
1329 			 */
1330 			if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
1331 				SCARG(&pta, addr) = (caddr_t) 1;
1332 
1333 			error = sys_ptrace(l, &pta, retval);
1334 			if (error)
1335 				return error;
1336 			switch (request) {
1337 			case LINUX_PTRACE_PEEKTEXT:
1338 			case LINUX_PTRACE_PEEKDATA:
1339 				error = copyout (retval,
1340 				    (caddr_t)SCARG(uap, data), sizeof *retval);
1341 				*retval = SCARG(uap, data);
1342 				break;
1343 			default:
1344 				break;
1345 			}
1346 			return error;
1347 		}
1348 		else
1349 			ptr++;
1350 
1351 	return LINUX_SYS_PTRACE_ARCH(l, uap, retval);
1352 }
1353 
1354 int
1355 linux_sys_reboot(struct lwp *l, void *v, register_t *retval)
1356 {
1357 	struct linux_sys_reboot_args /* {
1358 		syscallarg(int) magic1;
1359 		syscallarg(int) magic2;
1360 		syscallarg(int) cmd;
1361 		syscallarg(void *) arg;
1362 	} */ *uap = v;
1363 	struct sys_reboot_args /* {
1364 		syscallarg(int) opt;
1365 		syscallarg(char *) bootstr;
1366 	} */ sra;
1367 	struct proc *p = l->l_proc;
1368 	int error;
1369 
1370 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1371 		return(error);
1372 
1373 	if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
1374 		return(EINVAL);
1375 	if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
1376 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
1377 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
1378 		return(EINVAL);
1379 
1380 	switch (SCARG(uap, cmd)) {
1381 	case LINUX_REBOOT_CMD_RESTART:
1382 		SCARG(&sra, opt) = RB_AUTOBOOT;
1383 		break;
1384 	case LINUX_REBOOT_CMD_HALT:
1385 		SCARG(&sra, opt) = RB_HALT;
1386 		break;
1387 	case LINUX_REBOOT_CMD_POWER_OFF:
1388 		SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
1389 		break;
1390 	case LINUX_REBOOT_CMD_RESTART2:
1391 		/* Reboot with an argument. */
1392 		SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
1393 		SCARG(&sra, bootstr) = SCARG(uap, arg);
1394 		break;
1395 	case LINUX_REBOOT_CMD_CAD_ON:
1396 		return(EINVAL);	/* We don't implement ctrl-alt-delete */
1397 	case LINUX_REBOOT_CMD_CAD_OFF:
1398 		return(0);
1399 	default:
1400 		return(EINVAL);
1401 	}
1402 
1403 	return(sys_reboot(l, &sra, retval));
1404 }
1405 
1406 /*
1407  * Copy of compat_12_sys_swapon().
1408  */
1409 int
1410 linux_sys_swapon(l, v, retval)
1411 	struct lwp *l;
1412 	void *v;
1413 	register_t *retval;
1414 {
1415 	struct sys_swapctl_args ua;
1416 	struct linux_sys_swapon_args /* {
1417 		syscallarg(const char *) name;
1418 	} */ *uap = v;
1419 
1420 	SCARG(&ua, cmd) = SWAP_ON;
1421 	SCARG(&ua, arg) = (void *)SCARG(uap, name);
1422 	SCARG(&ua, misc) = 0;	/* priority */
1423 	return (sys_swapctl(l, &ua, retval));
1424 }
1425 
1426 /*
1427  * Stop swapping to the file or block device specified by path.
1428  */
1429 int
1430 linux_sys_swapoff(l, v, retval)
1431 	struct lwp *l;
1432 	void *v;
1433 	register_t *retval;
1434 {
1435 	struct sys_swapctl_args ua;
1436 	struct linux_sys_swapoff_args /* {
1437 		syscallarg(const char *) path;
1438 	} */ *uap = v;
1439 
1440 	SCARG(&ua, cmd) = SWAP_OFF;
1441 	SCARG(&ua, arg) = (void *)SCARG(uap, path);
1442 	return (sys_swapctl(l, &ua, retval));
1443 }
1444 
1445 /*
1446  * Copy of compat_09_sys_setdomainname()
1447  */
1448 /* ARGSUSED */
1449 int
1450 linux_sys_setdomainname(l, v, retval)
1451 	struct lwp *l;
1452 	void *v;
1453 	register_t *retval;
1454 {
1455 	struct linux_sys_setdomainname_args /* {
1456 		syscallarg(char *) domainname;
1457 		syscallarg(int) len;
1458 	} */ *uap = v;
1459 	struct proc *p = l->l_proc;
1460 	int name;
1461 	int error;
1462 
1463 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1464 		return (error);
1465 	name = KERN_DOMAINNAME;
1466 	return (kern_sysctl(&name, 1, 0, 0, SCARG(uap, domainname),
1467 			    SCARG(uap, len), p));
1468 }
1469 
1470 /*
1471  * sysinfo()
1472  */
1473 /* ARGSUSED */
1474 int
1475 linux_sys_sysinfo(l, v, retval)
1476 	struct lwp *l;
1477 	void *v;
1478 	register_t *retval;
1479 {
1480 	struct linux_sys_sysinfo_args /* {
1481 		syscallarg(struct linux_sysinfo *) arg;
1482 	} */ *uap = v;
1483 	struct linux_sysinfo si;
1484 	struct loadavg *la;
1485 
1486 	si.uptime = time.tv_sec - boottime.tv_sec;
1487 	la = &averunnable;
1488 	si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1489 	si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1490 	si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1491 	si.totalram = ctob(physmem);
1492 	si.freeram = uvmexp.free * uvmexp.pagesize;
1493 	si.sharedram = 0;	/* XXX */
1494 	si.bufferram = uvmexp.filepages * uvmexp.pagesize;
1495 	si.totalswap = uvmexp.swpages * uvmexp.pagesize;
1496 	si.freeswap = (uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
1497 	si.procs = nprocs;
1498 
1499 	/* The following are only present in newer Linux kernels. */
1500 	si.totalbig = 0;
1501 	si.freebig = 0;
1502 	si.mem_unit = 1;
1503 
1504 	return (copyout(&si, SCARG(uap, arg), sizeof si));
1505 }
1506 
1507 #define bsd_to_linux_rlimit1(l, b, f) \
1508     (l)->f = ((b)->f == RLIM_INFINITY || \
1509 	     ((b)->f & 0xffffffff00000000ULL) != 0) ? \
1510     LINUX_RLIM_INFINITY : (int32_t)(b)->f
1511 #define bsd_to_linux_rlimit(l, b) \
1512     bsd_to_linux_rlimit1(l, b, rlim_cur); \
1513     bsd_to_linux_rlimit1(l, b, rlim_max)
1514 
1515 #define linux_to_bsd_rlimit1(b, l, f) \
1516     (b)->f = (l)->f == LINUX_RLIM_INFINITY ? RLIM_INFINITY : (l)->f
1517 #define linux_to_bsd_rlimit(b, l) \
1518     linux_to_bsd_rlimit1(b, l, rlim_cur); \
1519     linux_to_bsd_rlimit1(b, l, rlim_max)
1520 
1521 static int
1522 linux_to_bsd_limit(lim)
1523 	int lim;
1524 {
1525 	switch (lim) {
1526 	case LINUX_RLIMIT_CPU:
1527 		return RLIMIT_CPU;
1528 	case LINUX_RLIMIT_FSIZE:
1529 		return RLIMIT_FSIZE;
1530 	case LINUX_RLIMIT_DATA:
1531 		return RLIMIT_DATA;
1532 	case LINUX_RLIMIT_STACK:
1533 		return RLIMIT_STACK;
1534 	case LINUX_RLIMIT_CORE:
1535 		return RLIMIT_CORE;
1536 	case LINUX_RLIMIT_RSS:
1537 		return RLIMIT_RSS;
1538 	case LINUX_RLIMIT_NPROC:
1539 		return RLIMIT_NPROC;
1540 	case LINUX_RLIMIT_NOFILE:
1541 		return RLIMIT_NOFILE;
1542 	case LINUX_RLIMIT_MEMLOCK:
1543 		return RLIMIT_MEMLOCK;
1544 	case LINUX_RLIMIT_AS:
1545 	case LINUX_RLIMIT_LOCKS:
1546 		return -EOPNOTSUPP;
1547 	default:
1548 		return -EINVAL;
1549 	}
1550 }
1551 
1552 
1553 int
1554 linux_sys_getrlimit(l, v, retval)
1555 	struct lwp *l;
1556 	void *v;
1557 	register_t *retval;
1558 {
1559 	struct linux_sys_getrlimit_args /* {
1560 		syscallarg(int) which;
1561 		syscallarg(struct orlimit *) rlp;
1562 	} */ *uap = v;
1563 	struct proc *p = l->l_proc;
1564 	caddr_t sg = stackgap_init(p, 0);
1565 	struct sys_getrlimit_args ap;
1566 	struct rlimit rl;
1567 	struct orlimit orl;
1568 	int error;
1569 
1570 	SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
1571 	if ((error = SCARG(&ap, which)) < 0)
1572 		return -error;
1573 	SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl);
1574 	if ((error = sys_getrlimit(l, &ap, retval)) != 0)
1575 		return error;
1576 	if ((error = copyin(SCARG(&ap, rlp), &rl, sizeof(rl))) != 0)
1577 		return error;
1578 	bsd_to_linux_rlimit(&orl, &rl);
1579 	return copyout(&orl, SCARG(uap, rlp), sizeof(orl));
1580 }
1581 
1582 int
1583 linux_sys_setrlimit(l, v, retval)
1584 	struct lwp *l;
1585 	void *v;
1586 	register_t *retval;
1587 {
1588 	struct linux_sys_setrlimit_args /* {
1589 		syscallarg(int) which;
1590 		syscallarg(struct orlimit *) rlp;
1591 	} */ *uap = v;
1592 	struct proc *p = l->l_proc;
1593 	caddr_t sg = stackgap_init(p, 0);
1594 	struct sys_setrlimit_args ap;
1595 	struct rlimit rl;
1596 	struct orlimit orl;
1597 	int error;
1598 
1599 	SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
1600 	SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl);
1601 	if ((error = SCARG(&ap, which)) < 0)
1602 		return -error;
1603 	if ((error = copyin(SCARG(uap, rlp), &orl, sizeof(orl))) != 0)
1604 		return error;
1605 	linux_to_bsd_rlimit(&rl, &orl);
1606 	/* XXX: alpha complains about this */
1607 	if ((error = copyout(&rl, (void *)SCARG(&ap, rlp), sizeof(rl))) != 0)
1608 		return error;
1609 	return sys_setrlimit(l, &ap, retval);
1610 }
1611 
1612 #ifndef __mips__
1613 /* XXX: this doesn't look 100% common, at least mips doesn't have it */
1614 int
1615 linux_sys_ugetrlimit(l, v, retval)
1616 	struct lwp *l;
1617 	void *v;
1618 	register_t *retval;
1619 {
1620 	return linux_sys_getrlimit(l, v, retval);
1621 }
1622 #endif
1623 
1624 /*
1625  * This gets called for unsupported syscalls. The difference to sys_nosys()
1626  * is that process does not get SIGSYS, the call just returns with ENOSYS.
1627  * This is the way Linux does it and glibc depends on this behaviour.
1628  */
1629 int
1630 linux_sys_nosys(l, v, retval)
1631 	struct lwp *l;
1632 	void *v;
1633 	register_t *retval;
1634 {
1635 	return (ENOSYS);
1636 }
1637