xref: /freebsd/sys/compat/linux/linux_misc.c (revision 480f4e946db51c7de558c4cd1ba3d88caeaceec8)
1 /*-
2  * Copyright (c) 2002 Doug Rabson
3  * Copyright (c) 1994-1995 Søren Schmidt
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer
11  *    in this position and unchanged.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include "opt_compat.h"
34 
35 #include <sys/param.h>
36 #include <sys/blist.h>
37 #include <sys/fcntl.h>
38 #if defined(__i386__)
39 #include <sys/imgact_aout.h>
40 #endif
41 #include <sys/jail.h>
42 #include <sys/kernel.h>
43 #include <sys/limits.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/mman.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/namei.h>
50 #include <sys/priv.h>
51 #include <sys/proc.h>
52 #include <sys/reboot.h>
53 #include <sys/racct.h>
54 #include <sys/resourcevar.h>
55 #include <sys/sched.h>
56 #include <sys/sdt.h>
57 #include <sys/signalvar.h>
58 #include <sys/stat.h>
59 #include <sys/syscallsubr.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysproto.h>
62 #include <sys/systm.h>
63 #include <sys/time.h>
64 #include <sys/vmmeter.h>
65 #include <sys/vnode.h>
66 #include <sys/wait.h>
67 #include <sys/cpuset.h>
68 
69 #include <security/mac/mac_framework.h>
70 
71 #include <vm/vm.h>
72 #include <vm/pmap.h>
73 #include <vm/vm_kern.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_extern.h>
76 #include <vm/vm_object.h>
77 #include <vm/swap_pager.h>
78 
79 #ifdef COMPAT_LINUX32
80 #include <machine/../linux32/linux.h>
81 #include <machine/../linux32/linux32_proto.h>
82 #else
83 #include <machine/../linux/linux.h>
84 #include <machine/../linux/linux_proto.h>
85 #endif
86 
87 #include <compat/linux/linux_dtrace.h>
88 #include <compat/linux/linux_file.h>
89 #include <compat/linux/linux_mib.h>
90 #include <compat/linux/linux_signal.h>
91 #include <compat/linux/linux_timer.h>
92 #include <compat/linux/linux_util.h>
93 #include <compat/linux/linux_sysproto.h>
94 #include <compat/linux/linux_emul.h>
95 #include <compat/linux/linux_misc.h>
96 
97 /**
98  * Special DTrace provider for the linuxulator.
99  *
100  * In this file we define the provider for the entire linuxulator. All
101  * modules (= files of the linuxulator) use it.
102  *
103  * We define a different name depending on the emulated bitsize, see
104  * ../../<ARCH>/linux{,32}/linux.h, e.g.:
105  *      native bitsize          = linuxulator
106  *      amd64, 32bit emulation  = linuxulator32
107  */
108 LIN_SDT_PROVIDER_DEFINE(LINUX_DTRACE);
109 
110 int stclohz;				/* Statistics clock frequency */
111 
112 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
113 	RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
114 	RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
115 	RLIMIT_MEMLOCK, RLIMIT_AS
116 };
117 
118 struct l_sysinfo {
119 	l_long		uptime;		/* Seconds since boot */
120 	l_ulong		loads[3];	/* 1, 5, and 15 minute load averages */
121 #define LINUX_SYSINFO_LOADS_SCALE 65536
122 	l_ulong		totalram;	/* Total usable main memory size */
123 	l_ulong		freeram;	/* Available memory size */
124 	l_ulong		sharedram;	/* Amount of shared memory */
125 	l_ulong		bufferram;	/* Memory used by buffers */
126 	l_ulong		totalswap;	/* Total swap space size */
127 	l_ulong		freeswap;	/* swap space still available */
128 	l_ushort	procs;		/* Number of current processes */
129 	l_ushort	pads;
130 	l_ulong		totalbig;
131 	l_ulong		freebig;
132 	l_uint		mem_unit;
133 	char		_f[20-2*sizeof(l_long)-sizeof(l_int)];	/* padding */
134 };
135 
136 struct l_pselect6arg {
137 	l_uintptr_t	ss;
138 	l_size_t	ss_len;
139 };
140 
141 static int	linux_utimensat_nsec_valid(l_long);
142 
143 
144 int
145 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
146 {
147 	struct l_sysinfo sysinfo;
148 	vm_object_t object;
149 	int i, j;
150 	struct timespec ts;
151 
152 	getnanouptime(&ts);
153 	if (ts.tv_nsec != 0)
154 		ts.tv_sec++;
155 	sysinfo.uptime = ts.tv_sec;
156 
157 	/* Use the information from the mib to get our load averages */
158 	for (i = 0; i < 3; i++)
159 		sysinfo.loads[i] = averunnable.ldavg[i] *
160 		    LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
161 
162 	sysinfo.totalram = physmem * PAGE_SIZE;
163 	sysinfo.freeram = sysinfo.totalram - vm_cnt.v_wire_count * PAGE_SIZE;
164 
165 	sysinfo.sharedram = 0;
166 	mtx_lock(&vm_object_list_mtx);
167 	TAILQ_FOREACH(object, &vm_object_list, object_list)
168 		if (object->shadow_count > 1)
169 			sysinfo.sharedram += object->resident_page_count;
170 	mtx_unlock(&vm_object_list_mtx);
171 
172 	sysinfo.sharedram *= PAGE_SIZE;
173 	sysinfo.bufferram = 0;
174 
175 	swap_pager_status(&i, &j);
176 	sysinfo.totalswap = i * PAGE_SIZE;
177 	sysinfo.freeswap = (i - j) * PAGE_SIZE;
178 
179 	sysinfo.procs = nprocs;
180 
181 	/* The following are only present in newer Linux kernels. */
182 	sysinfo.totalbig = 0;
183 	sysinfo.freebig = 0;
184 	sysinfo.mem_unit = 1;
185 
186 	return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
187 }
188 
189 int
190 linux_alarm(struct thread *td, struct linux_alarm_args *args)
191 {
192 	struct itimerval it, old_it;
193 	u_int secs;
194 	int error;
195 
196 #ifdef DEBUG
197 	if (ldebug(alarm))
198 		printf(ARGS(alarm, "%u"), args->secs);
199 #endif
200 	secs = args->secs;
201 	/*
202 	 * Linux alarm() is always successfull. Limit secs to INT32_MAX / 2
203 	 * to match kern_setitimer()'s limit to avoid error from it.
204 	 *
205 	 * XXX. Linux limit secs to INT_MAX on 32 and does not limit on 64-bit
206 	 * platforms.
207 	 */
208 	if (secs > INT32_MAX / 2)
209 		secs = INT32_MAX / 2;
210 
211 	it.it_value.tv_sec = secs;
212 	it.it_value.tv_usec = 0;
213 	timevalclear(&it.it_interval);
214 	error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
215 	KASSERT(error == 0, ("kern_setitimer returns %d", error));
216 
217 	if ((old_it.it_value.tv_sec == 0 && old_it.it_value.tv_usec > 0) ||
218 	    old_it.it_value.tv_usec >= 500000)
219 		old_it.it_value.tv_sec++;
220 	td->td_retval[0] = old_it.it_value.tv_sec;
221 	return (0);
222 }
223 
224 int
225 linux_brk(struct thread *td, struct linux_brk_args *args)
226 {
227 	struct vmspace *vm = td->td_proc->p_vmspace;
228 	vm_offset_t new, old;
229 	struct obreak_args /* {
230 		char * nsize;
231 	} */ tmp;
232 
233 #ifdef DEBUG
234 	if (ldebug(brk))
235 		printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
236 #endif
237 	old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
238 	new = (vm_offset_t)args->dsend;
239 	tmp.nsize = (char *)new;
240 	if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(td, &tmp))
241 		td->td_retval[0] = (long)new;
242 	else
243 		td->td_retval[0] = (long)old;
244 
245 	return (0);
246 }
247 
248 #if defined(__i386__)
249 /* XXX: what about amd64/linux32? */
250 
251 int
252 linux_uselib(struct thread *td, struct linux_uselib_args *args)
253 {
254 	struct nameidata ni;
255 	struct vnode *vp;
256 	struct exec *a_out;
257 	struct vattr attr;
258 	vm_offset_t vmaddr;
259 	unsigned long file_offset;
260 	unsigned long bss_size;
261 	char *library;
262 	ssize_t aresid;
263 	int error, locked, writecount;
264 
265 	LCONVPATHEXIST(td, args->library, &library);
266 
267 #ifdef DEBUG
268 	if (ldebug(uselib))
269 		printf(ARGS(uselib, "%s"), library);
270 #endif
271 
272 	a_out = NULL;
273 	locked = 0;
274 	vp = NULL;
275 
276 	NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
277 	    UIO_SYSSPACE, library, td);
278 	error = namei(&ni);
279 	LFREEPATH(library);
280 	if (error)
281 		goto cleanup;
282 
283 	vp = ni.ni_vp;
284 	NDFREE(&ni, NDF_ONLY_PNBUF);
285 
286 	/*
287 	 * From here on down, we have a locked vnode that must be unlocked.
288 	 * XXX: The code below largely duplicates exec_check_permissions().
289 	 */
290 	locked = 1;
291 
292 	/* Writable? */
293 	error = VOP_GET_WRITECOUNT(vp, &writecount);
294 	if (error != 0)
295 		goto cleanup;
296 	if (writecount != 0) {
297 		error = ETXTBSY;
298 		goto cleanup;
299 	}
300 
301 	/* Executable? */
302 	error = VOP_GETATTR(vp, &attr, td->td_ucred);
303 	if (error)
304 		goto cleanup;
305 
306 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
307 	    ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
308 		/* EACCESS is what exec(2) returns. */
309 		error = ENOEXEC;
310 		goto cleanup;
311 	}
312 
313 	/* Sensible size? */
314 	if (attr.va_size == 0) {
315 		error = ENOEXEC;
316 		goto cleanup;
317 	}
318 
319 	/* Can we access it? */
320 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
321 	if (error)
322 		goto cleanup;
323 
324 	/*
325 	 * XXX: This should use vn_open() so that it is properly authorized,
326 	 * and to reduce code redundancy all over the place here.
327 	 * XXX: Not really, it duplicates far more of exec_check_permissions()
328 	 * than vn_open().
329 	 */
330 #ifdef MAC
331 	error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
332 	if (error)
333 		goto cleanup;
334 #endif
335 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
336 	if (error)
337 		goto cleanup;
338 
339 	/* Pull in executable header into exec_map */
340 	error = vm_mmap(exec_map, (vm_offset_t *)&a_out, PAGE_SIZE,
341 	    VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
342 	if (error)
343 		goto cleanup;
344 
345 	/* Is it a Linux binary ? */
346 	if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
347 		error = ENOEXEC;
348 		goto cleanup;
349 	}
350 
351 	/*
352 	 * While we are here, we should REALLY do some more checks
353 	 */
354 
355 	/* Set file/virtual offset based on a.out variant. */
356 	switch ((int)(a_out->a_magic & 0xffff)) {
357 	case 0413:			/* ZMAGIC */
358 		file_offset = 1024;
359 		break;
360 	case 0314:			/* QMAGIC */
361 		file_offset = 0;
362 		break;
363 	default:
364 		error = ENOEXEC;
365 		goto cleanup;
366 	}
367 
368 	bss_size = round_page(a_out->a_bss);
369 
370 	/* Check various fields in header for validity/bounds. */
371 	if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
372 		error = ENOEXEC;
373 		goto cleanup;
374 	}
375 
376 	/* text + data can't exceed file size */
377 	if (a_out->a_data + a_out->a_text > attr.va_size) {
378 		error = EFAULT;
379 		goto cleanup;
380 	}
381 
382 	/*
383 	 * text/data/bss must not exceed limits
384 	 * XXX - this is not complete. it should check current usage PLUS
385 	 * the resources needed by this library.
386 	 */
387 	PROC_LOCK(td->td_proc);
388 	if (a_out->a_text > maxtsiz ||
389 	    a_out->a_data + bss_size > lim_cur_proc(td->td_proc, RLIMIT_DATA) ||
390 	    racct_set(td->td_proc, RACCT_DATA, a_out->a_data +
391 	    bss_size) != 0) {
392 		PROC_UNLOCK(td->td_proc);
393 		error = ENOMEM;
394 		goto cleanup;
395 	}
396 	PROC_UNLOCK(td->td_proc);
397 
398 	/*
399 	 * Prevent more writers.
400 	 * XXX: Note that if any of the VM operations fail below we don't
401 	 * clear this flag.
402 	 */
403 	VOP_SET_TEXT(vp);
404 
405 	/*
406 	 * Lock no longer needed
407 	 */
408 	locked = 0;
409 	VOP_UNLOCK(vp, 0);
410 
411 	/*
412 	 * Check if file_offset page aligned. Currently we cannot handle
413 	 * misalinged file offsets, and so we read in the entire image
414 	 * (what a waste).
415 	 */
416 	if (file_offset & PAGE_MASK) {
417 #ifdef DEBUG
418 		printf("uselib: Non page aligned binary %lu\n", file_offset);
419 #endif
420 		/* Map text+data read/write/execute */
421 
422 		/* a_entry is the load address and is page aligned */
423 		vmaddr = trunc_page(a_out->a_entry);
424 
425 		/* get anon user mapping, read+write+execute */
426 		error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
427 		    &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE,
428 		    VM_PROT_ALL, VM_PROT_ALL, 0);
429 		if (error)
430 			goto cleanup;
431 
432 		error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
433 		    a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
434 		    td->td_ucred, NOCRED, &aresid, td);
435 		if (error != 0)
436 			goto cleanup;
437 		if (aresid != 0) {
438 			error = ENOEXEC;
439 			goto cleanup;
440 		}
441 	} else {
442 #ifdef DEBUG
443 		printf("uselib: Page aligned binary %lu\n", file_offset);
444 #endif
445 		/*
446 		 * for QMAGIC, a_entry is 20 bytes beyond the load address
447 		 * to skip the executable header
448 		 */
449 		vmaddr = trunc_page(a_out->a_entry);
450 
451 		/*
452 		 * Map it all into the process's space as a single
453 		 * copy-on-write "data" segment.
454 		 */
455 		error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
456 		    a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
457 		    MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
458 		if (error)
459 			goto cleanup;
460 	}
461 #ifdef DEBUG
462 	printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
463 	    ((long *)vmaddr)[1]);
464 #endif
465 	if (bss_size != 0) {
466 		/* Calculate BSS start address */
467 		vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
468 		    a_out->a_data;
469 
470 		/* allocate some 'anon' space */
471 		error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
472 		    &vmaddr, bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL,
473 		    VM_PROT_ALL, 0);
474 		if (error)
475 			goto cleanup;
476 	}
477 
478 cleanup:
479 	/* Unlock vnode if needed */
480 	if (locked)
481 		VOP_UNLOCK(vp, 0);
482 
483 	/* Release the temporary mapping. */
484 	if (a_out)
485 		kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
486 
487 	return (error);
488 }
489 
490 #endif	/* __i386__ */
491 
492 int
493 linux_select(struct thread *td, struct linux_select_args *args)
494 {
495 	l_timeval ltv;
496 	struct timeval tv0, tv1, utv, *tvp;
497 	int error;
498 
499 #ifdef DEBUG
500 	if (ldebug(select))
501 		printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
502 		    (void *)args->readfds, (void *)args->writefds,
503 		    (void *)args->exceptfds, (void *)args->timeout);
504 #endif
505 
506 	/*
507 	 * Store current time for computation of the amount of
508 	 * time left.
509 	 */
510 	if (args->timeout) {
511 		if ((error = copyin(args->timeout, &ltv, sizeof(ltv))))
512 			goto select_out;
513 		utv.tv_sec = ltv.tv_sec;
514 		utv.tv_usec = ltv.tv_usec;
515 #ifdef DEBUG
516 		if (ldebug(select))
517 			printf(LMSG("incoming timeout (%jd/%ld)"),
518 			    (intmax_t)utv.tv_sec, utv.tv_usec);
519 #endif
520 
521 		if (itimerfix(&utv)) {
522 			/*
523 			 * The timeval was invalid.  Convert it to something
524 			 * valid that will act as it does under Linux.
525 			 */
526 			utv.tv_sec += utv.tv_usec / 1000000;
527 			utv.tv_usec %= 1000000;
528 			if (utv.tv_usec < 0) {
529 				utv.tv_sec -= 1;
530 				utv.tv_usec += 1000000;
531 			}
532 			if (utv.tv_sec < 0)
533 				timevalclear(&utv);
534 		}
535 		microtime(&tv0);
536 		tvp = &utv;
537 	} else
538 		tvp = NULL;
539 
540 	error = kern_select(td, args->nfds, args->readfds, args->writefds,
541 	    args->exceptfds, tvp, LINUX_NFDBITS);
542 
543 #ifdef DEBUG
544 	if (ldebug(select))
545 		printf(LMSG("real select returns %d"), error);
546 #endif
547 	if (error)
548 		goto select_out;
549 
550 	if (args->timeout) {
551 		if (td->td_retval[0]) {
552 			/*
553 			 * Compute how much time was left of the timeout,
554 			 * by subtracting the current time and the time
555 			 * before we started the call, and subtracting
556 			 * that result from the user-supplied value.
557 			 */
558 			microtime(&tv1);
559 			timevalsub(&tv1, &tv0);
560 			timevalsub(&utv, &tv1);
561 			if (utv.tv_sec < 0)
562 				timevalclear(&utv);
563 		} else
564 			timevalclear(&utv);
565 #ifdef DEBUG
566 		if (ldebug(select))
567 			printf(LMSG("outgoing timeout (%jd/%ld)"),
568 			    (intmax_t)utv.tv_sec, utv.tv_usec);
569 #endif
570 		ltv.tv_sec = utv.tv_sec;
571 		ltv.tv_usec = utv.tv_usec;
572 		if ((error = copyout(&ltv, args->timeout, sizeof(ltv))))
573 			goto select_out;
574 	}
575 
576 select_out:
577 #ifdef DEBUG
578 	if (ldebug(select))
579 		printf(LMSG("select_out -> %d"), error);
580 #endif
581 	return (error);
582 }
583 
584 int
585 linux_mremap(struct thread *td, struct linux_mremap_args *args)
586 {
587 	struct munmap_args /* {
588 		void *addr;
589 		size_t len;
590 	} */ bsd_args;
591 	int error = 0;
592 
593 #ifdef DEBUG
594 	if (ldebug(mremap))
595 		printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
596 		    (void *)(uintptr_t)args->addr,
597 		    (unsigned long)args->old_len,
598 		    (unsigned long)args->new_len,
599 		    (unsigned long)args->flags);
600 #endif
601 
602 	if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
603 		td->td_retval[0] = 0;
604 		return (EINVAL);
605 	}
606 
607 	/*
608 	 * Check for the page alignment.
609 	 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
610 	 */
611 	if (args->addr & PAGE_MASK) {
612 		td->td_retval[0] = 0;
613 		return (EINVAL);
614 	}
615 
616 	args->new_len = round_page(args->new_len);
617 	args->old_len = round_page(args->old_len);
618 
619 	if (args->new_len > args->old_len) {
620 		td->td_retval[0] = 0;
621 		return (ENOMEM);
622 	}
623 
624 	if (args->new_len < args->old_len) {
625 		bsd_args.addr =
626 		    (caddr_t)((uintptr_t)args->addr + args->new_len);
627 		bsd_args.len = args->old_len - args->new_len;
628 		error = sys_munmap(td, &bsd_args);
629 	}
630 
631 	td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
632 	return (error);
633 }
634 
635 #define LINUX_MS_ASYNC       0x0001
636 #define LINUX_MS_INVALIDATE  0x0002
637 #define LINUX_MS_SYNC        0x0004
638 
639 int
640 linux_msync(struct thread *td, struct linux_msync_args *args)
641 {
642 	struct msync_args bsd_args;
643 
644 	bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
645 	bsd_args.len = (uintptr_t)args->len;
646 	bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
647 
648 	return (sys_msync(td, &bsd_args));
649 }
650 
651 int
652 linux_time(struct thread *td, struct linux_time_args *args)
653 {
654 	struct timeval tv;
655 	l_time_t tm;
656 	int error;
657 
658 #ifdef DEBUG
659 	if (ldebug(time))
660 		printf(ARGS(time, "*"));
661 #endif
662 
663 	microtime(&tv);
664 	tm = tv.tv_sec;
665 	if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
666 		return (error);
667 	td->td_retval[0] = tm;
668 	return (0);
669 }
670 
671 struct l_times_argv {
672 	l_clock_t	tms_utime;
673 	l_clock_t	tms_stime;
674 	l_clock_t	tms_cutime;
675 	l_clock_t	tms_cstime;
676 };
677 
678 
679 /*
680  * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
681  * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
682  * auxiliary vector entry.
683  */
684 #define	CLK_TCK		100
685 
686 #define	CONVOTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
687 #define	CONVNTCK(r)	(r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
688 
689 #define	CONVTCK(r)	(linux_kernver(td) >= LINUX_KERNVER_2004000 ?		\
690 			    CONVNTCK(r) : CONVOTCK(r))
691 
692 int
693 linux_times(struct thread *td, struct linux_times_args *args)
694 {
695 	struct timeval tv, utime, stime, cutime, cstime;
696 	struct l_times_argv tms;
697 	struct proc *p;
698 	int error;
699 
700 #ifdef DEBUG
701 	if (ldebug(times))
702 		printf(ARGS(times, "*"));
703 #endif
704 
705 	if (args->buf != NULL) {
706 		p = td->td_proc;
707 		PROC_LOCK(p);
708 		PROC_STATLOCK(p);
709 		calcru(p, &utime, &stime);
710 		PROC_STATUNLOCK(p);
711 		calccru(p, &cutime, &cstime);
712 		PROC_UNLOCK(p);
713 
714 		tms.tms_utime = CONVTCK(utime);
715 		tms.tms_stime = CONVTCK(stime);
716 
717 		tms.tms_cutime = CONVTCK(cutime);
718 		tms.tms_cstime = CONVTCK(cstime);
719 
720 		if ((error = copyout(&tms, args->buf, sizeof(tms))))
721 			return (error);
722 	}
723 
724 	microuptime(&tv);
725 	td->td_retval[0] = (int)CONVTCK(tv);
726 	return (0);
727 }
728 
729 int
730 linux_newuname(struct thread *td, struct linux_newuname_args *args)
731 {
732 	struct l_new_utsname utsname;
733 	char osname[LINUX_MAX_UTSNAME];
734 	char osrelease[LINUX_MAX_UTSNAME];
735 	char *p;
736 
737 #ifdef DEBUG
738 	if (ldebug(newuname))
739 		printf(ARGS(newuname, "*"));
740 #endif
741 
742 	linux_get_osname(td, osname);
743 	linux_get_osrelease(td, osrelease);
744 
745 	bzero(&utsname, sizeof(utsname));
746 	strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
747 	getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
748 	getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
749 	strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
750 	strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
751 	for (p = utsname.version; *p != '\0'; ++p)
752 		if (*p == '\n') {
753 			*p = '\0';
754 			break;
755 		}
756 	strlcpy(utsname.machine, linux_kplatform, LINUX_MAX_UTSNAME);
757 
758 	return (copyout(&utsname, args->buf, sizeof(utsname)));
759 }
760 
761 struct l_utimbuf {
762 	l_time_t l_actime;
763 	l_time_t l_modtime;
764 };
765 
766 int
767 linux_utime(struct thread *td, struct linux_utime_args *args)
768 {
769 	struct timeval tv[2], *tvp;
770 	struct l_utimbuf lut;
771 	char *fname;
772 	int error;
773 
774 	LCONVPATHEXIST(td, args->fname, &fname);
775 
776 #ifdef DEBUG
777 	if (ldebug(utime))
778 		printf(ARGS(utime, "%s, *"), fname);
779 #endif
780 
781 	if (args->times) {
782 		if ((error = copyin(args->times, &lut, sizeof lut))) {
783 			LFREEPATH(fname);
784 			return (error);
785 		}
786 		tv[0].tv_sec = lut.l_actime;
787 		tv[0].tv_usec = 0;
788 		tv[1].tv_sec = lut.l_modtime;
789 		tv[1].tv_usec = 0;
790 		tvp = tv;
791 	} else
792 		tvp = NULL;
793 
794 	error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE, tvp,
795 	    UIO_SYSSPACE);
796 	LFREEPATH(fname);
797 	return (error);
798 }
799 
800 int
801 linux_utimes(struct thread *td, struct linux_utimes_args *args)
802 {
803 	l_timeval ltv[2];
804 	struct timeval tv[2], *tvp = NULL;
805 	char *fname;
806 	int error;
807 
808 	LCONVPATHEXIST(td, args->fname, &fname);
809 
810 #ifdef DEBUG
811 	if (ldebug(utimes))
812 		printf(ARGS(utimes, "%s, *"), fname);
813 #endif
814 
815 	if (args->tptr != NULL) {
816 		if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
817 			LFREEPATH(fname);
818 			return (error);
819 		}
820 		tv[0].tv_sec = ltv[0].tv_sec;
821 		tv[0].tv_usec = ltv[0].tv_usec;
822 		tv[1].tv_sec = ltv[1].tv_sec;
823 		tv[1].tv_usec = ltv[1].tv_usec;
824 		tvp = tv;
825 	}
826 
827 	error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE,
828 	    tvp, UIO_SYSSPACE);
829 	LFREEPATH(fname);
830 	return (error);
831 }
832 
833 static int
834 linux_utimensat_nsec_valid(l_long nsec)
835 {
836 
837 	if (nsec == LINUX_UTIME_OMIT || nsec == LINUX_UTIME_NOW)
838 		return (0);
839 	if (nsec >= 0 && nsec <= 999999999)
840 		return (0);
841 	return (1);
842 }
843 
844 int
845 linux_utimensat(struct thread *td, struct linux_utimensat_args *args)
846 {
847 	struct l_timespec l_times[2];
848 	struct timespec times[2], *timesp = NULL;
849 	char *path = NULL;
850 	int error, dfd, flags = 0;
851 
852 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
853 
854 #ifdef DEBUG
855 	if (ldebug(utimensat))
856 		printf(ARGS(utimensat, "%d, *"), dfd);
857 #endif
858 
859 	if (args->flags & ~LINUX_AT_SYMLINK_NOFOLLOW)
860 		return (EINVAL);
861 
862 	if (args->times != NULL) {
863 		error = copyin(args->times, l_times, sizeof(l_times));
864 		if (error != 0)
865 			return (error);
866 
867 		if (linux_utimensat_nsec_valid(l_times[0].tv_nsec) != 0 ||
868 		    linux_utimensat_nsec_valid(l_times[1].tv_nsec) != 0)
869 			return (EINVAL);
870 
871 		times[0].tv_sec = l_times[0].tv_sec;
872 		switch (l_times[0].tv_nsec)
873 		{
874 		case LINUX_UTIME_OMIT:
875 			times[0].tv_nsec = UTIME_OMIT;
876 			break;
877 		case LINUX_UTIME_NOW:
878 			times[0].tv_nsec = UTIME_NOW;
879 			break;
880 		default:
881 			times[0].tv_nsec = l_times[0].tv_nsec;
882 		}
883 
884 		times[1].tv_sec = l_times[1].tv_sec;
885 		switch (l_times[1].tv_nsec)
886 		{
887 		case LINUX_UTIME_OMIT:
888 			times[1].tv_nsec = UTIME_OMIT;
889 			break;
890 		case LINUX_UTIME_NOW:
891 			times[1].tv_nsec = UTIME_NOW;
892 			break;
893 		default:
894 			times[1].tv_nsec = l_times[1].tv_nsec;
895 			break;
896 		}
897 		timesp = times;
898 	}
899 
900 	if (times[0].tv_nsec == UTIME_OMIT && times[1].tv_nsec == UTIME_OMIT)
901 		/* This breaks POSIX, but is what the Linux kernel does
902 		 * _on purpose_ (documented in the man page for utimensat(2)),
903 		 * so we must follow that behaviour. */
904 		return (0);
905 
906 	if (args->pathname != NULL)
907 		LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
908 	else if (args->flags != 0)
909 		return (EINVAL);
910 
911 	if (args->flags & LINUX_AT_SYMLINK_NOFOLLOW)
912 		flags |= AT_SYMLINK_NOFOLLOW;
913 
914 	if (path == NULL)
915 		error = kern_futimens(td, dfd, timesp, UIO_SYSSPACE);
916 	else {
917 		error = kern_utimensat(td, dfd, path, UIO_SYSSPACE, timesp,
918 	    		UIO_SYSSPACE, flags);
919 		LFREEPATH(path);
920 	}
921 
922 	return (error);
923 }
924 
925 int
926 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
927 {
928 	l_timeval ltv[2];
929 	struct timeval tv[2], *tvp = NULL;
930 	char *fname;
931 	int error, dfd;
932 
933 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
934 	LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
935 
936 #ifdef DEBUG
937 	if (ldebug(futimesat))
938 		printf(ARGS(futimesat, "%s, *"), fname);
939 #endif
940 
941 	if (args->utimes != NULL) {
942 		if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
943 			LFREEPATH(fname);
944 			return (error);
945 		}
946 		tv[0].tv_sec = ltv[0].tv_sec;
947 		tv[0].tv_usec = ltv[0].tv_usec;
948 		tv[1].tv_sec = ltv[1].tv_sec;
949 		tv[1].tv_usec = ltv[1].tv_usec;
950 		tvp = tv;
951 	}
952 
953 	error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
954 	LFREEPATH(fname);
955 	return (error);
956 }
957 
958 int
959 linux_common_wait(struct thread *td, int pid, int *status,
960     int options, struct rusage *ru)
961 {
962 	int error, tmpstat;
963 
964 	error = kern_wait(td, pid, &tmpstat, options, ru);
965 	if (error)
966 		return (error);
967 
968 	if (status) {
969 		tmpstat &= 0xffff;
970 		if (WIFSIGNALED(tmpstat))
971 			tmpstat = (tmpstat & 0xffffff80) |
972 			    bsd_to_linux_signal(WTERMSIG(tmpstat));
973 		else if (WIFSTOPPED(tmpstat))
974 			tmpstat = (tmpstat & 0xffff00ff) |
975 			    (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8);
976 		else if (WIFCONTINUED(tmpstat))
977 			tmpstat = 0xffff;
978 		error = copyout(&tmpstat, status, sizeof(int));
979 	}
980 
981 	return (error);
982 }
983 
984 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
985 int
986 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
987 {
988 	struct linux_wait4_args wait4_args;
989 
990 #ifdef DEBUG
991 	if (ldebug(waitpid))
992 		printf(ARGS(waitpid, "%d, %p, %d"),
993 		    args->pid, (void *)args->status, args->options);
994 #endif
995 
996 	wait4_args.pid = args->pid;
997 	wait4_args.status = args->status;
998 	wait4_args.options = args->options;
999 	wait4_args.rusage = NULL;
1000 
1001 	return (linux_wait4(td, &wait4_args));
1002 }
1003 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1004 
1005 int
1006 linux_wait4(struct thread *td, struct linux_wait4_args *args)
1007 {
1008 	int error, options;
1009 	struct rusage ru, *rup;
1010 
1011 #ifdef DEBUG
1012 	if (ldebug(wait4))
1013 		printf(ARGS(wait4, "%d, %p, %d, %p"),
1014 		    args->pid, (void *)args->status, args->options,
1015 		    (void *)args->rusage);
1016 #endif
1017 	if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG |
1018 	    LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
1019 		return (EINVAL);
1020 
1021 	options = WEXITED;
1022 	linux_to_bsd_waitopts(args->options, &options);
1023 
1024 	if (args->rusage != NULL)
1025 		rup = &ru;
1026 	else
1027 		rup = NULL;
1028 	error = linux_common_wait(td, args->pid, args->status, options, rup);
1029 	if (error != 0)
1030 		return (error);
1031 	if (args->rusage != NULL)
1032 		error = linux_copyout_rusage(&ru, args->rusage);
1033 	return (error);
1034 }
1035 
1036 int
1037 linux_waitid(struct thread *td, struct linux_waitid_args *args)
1038 {
1039 	int status, options, sig;
1040 	struct __wrusage wru;
1041 	siginfo_t siginfo;
1042 	l_siginfo_t lsi;
1043 	idtype_t idtype;
1044 	struct proc *p;
1045 	int error;
1046 
1047 	options = 0;
1048 	linux_to_bsd_waitopts(args->options, &options);
1049 
1050 	if (options & ~(WNOHANG | WNOWAIT | WEXITED | WUNTRACED | WCONTINUED))
1051 		return (EINVAL);
1052 	if (!(options & (WEXITED | WUNTRACED | WCONTINUED)))
1053 		return (EINVAL);
1054 
1055 	switch (args->idtype) {
1056 	case LINUX_P_ALL:
1057 		idtype = P_ALL;
1058 		break;
1059 	case LINUX_P_PID:
1060 		if (args->id <= 0)
1061 			return (EINVAL);
1062 		idtype = P_PID;
1063 		break;
1064 	case LINUX_P_PGID:
1065 		if (args->id <= 0)
1066 			return (EINVAL);
1067 		idtype = P_PGID;
1068 		break;
1069 	default:
1070 		return (EINVAL);
1071 	}
1072 
1073 	error = kern_wait6(td, idtype, args->id, &status, options,
1074 	    &wru, &siginfo);
1075 	if (error != 0)
1076 		return (error);
1077 	if (args->rusage != NULL) {
1078 		error = linux_copyout_rusage(&wru.wru_children,
1079 		    args->rusage);
1080 		if (error != 0)
1081 			return (error);
1082 	}
1083 	if (args->info != NULL) {
1084 		p = td->td_proc;
1085 		if (td->td_retval[0] == 0)
1086 			bzero(&lsi, sizeof(lsi));
1087 		else {
1088 			sig = bsd_to_linux_signal(siginfo.si_signo);
1089 			siginfo_to_lsiginfo(&siginfo, &lsi, sig);
1090 		}
1091 		error = copyout(&lsi, args->info, sizeof(lsi));
1092 	}
1093 	td->td_retval[0] = 0;
1094 
1095 	return (error);
1096 }
1097 
1098 int
1099 linux_mknod(struct thread *td, struct linux_mknod_args *args)
1100 {
1101 	char *path;
1102 	int error;
1103 
1104 	LCONVPATHCREAT(td, args->path, &path);
1105 
1106 #ifdef DEBUG
1107 	if (ldebug(mknod))
1108 		printf(ARGS(mknod, "%s, %d, %ju"), path, args->mode,
1109 		    (uintmax_t)args->dev);
1110 #endif
1111 
1112 	switch (args->mode & S_IFMT) {
1113 	case S_IFIFO:
1114 	case S_IFSOCK:
1115 		error = kern_mkfifoat(td, AT_FDCWD, path, UIO_SYSSPACE,
1116 		    args->mode);
1117 		break;
1118 
1119 	case S_IFCHR:
1120 	case S_IFBLK:
1121 		error = kern_mknodat(td, AT_FDCWD, path, UIO_SYSSPACE,
1122 		    args->mode, args->dev);
1123 		break;
1124 
1125 	case S_IFDIR:
1126 		error = EPERM;
1127 		break;
1128 
1129 	case 0:
1130 		args->mode |= S_IFREG;
1131 		/* FALLTHROUGH */
1132 	case S_IFREG:
1133 		error = kern_openat(td, AT_FDCWD, path, UIO_SYSSPACE,
1134 		    O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1135 		if (error == 0)
1136 			kern_close(td, td->td_retval[0]);
1137 		break;
1138 
1139 	default:
1140 		error = EINVAL;
1141 		break;
1142 	}
1143 	LFREEPATH(path);
1144 	return (error);
1145 }
1146 
1147 int
1148 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
1149 {
1150 	char *path;
1151 	int error, dfd;
1152 
1153 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1154 	LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
1155 
1156 #ifdef DEBUG
1157 	if (ldebug(mknodat))
1158 		printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev);
1159 #endif
1160 
1161 	switch (args->mode & S_IFMT) {
1162 	case S_IFIFO:
1163 	case S_IFSOCK:
1164 		error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
1165 		break;
1166 
1167 	case S_IFCHR:
1168 	case S_IFBLK:
1169 		error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
1170 		    args->dev);
1171 		break;
1172 
1173 	case S_IFDIR:
1174 		error = EPERM;
1175 		break;
1176 
1177 	case 0:
1178 		args->mode |= S_IFREG;
1179 		/* FALLTHROUGH */
1180 	case S_IFREG:
1181 		error = kern_openat(td, dfd, path, UIO_SYSSPACE,
1182 		    O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1183 		if (error == 0)
1184 			kern_close(td, td->td_retval[0]);
1185 		break;
1186 
1187 	default:
1188 		error = EINVAL;
1189 		break;
1190 	}
1191 	LFREEPATH(path);
1192 	return (error);
1193 }
1194 
1195 /*
1196  * UGH! This is just about the dumbest idea I've ever heard!!
1197  */
1198 int
1199 linux_personality(struct thread *td, struct linux_personality_args *args)
1200 {
1201 #ifdef DEBUG
1202 	if (ldebug(personality))
1203 		printf(ARGS(personality, "%lu"), (unsigned long)args->per);
1204 #endif
1205 	if (args->per != 0)
1206 		return (EINVAL);
1207 
1208 	/* Yes Jim, it's still a Linux... */
1209 	td->td_retval[0] = 0;
1210 	return (0);
1211 }
1212 
1213 struct l_itimerval {
1214 	l_timeval it_interval;
1215 	l_timeval it_value;
1216 };
1217 
1218 #define	B2L_ITIMERVAL(bip, lip) 					\
1219 	(bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec;		\
1220 	(bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec;	\
1221 	(bip)->it_value.tv_sec = (lip)->it_value.tv_sec;		\
1222 	(bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1223 
1224 int
1225 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1226 {
1227 	int error;
1228 	struct l_itimerval ls;
1229 	struct itimerval aitv, oitv;
1230 
1231 #ifdef DEBUG
1232 	if (ldebug(setitimer))
1233 		printf(ARGS(setitimer, "%p, %p"),
1234 		    (void *)uap->itv, (void *)uap->oitv);
1235 #endif
1236 
1237 	if (uap->itv == NULL) {
1238 		uap->itv = uap->oitv;
1239 		return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1240 	}
1241 
1242 	error = copyin(uap->itv, &ls, sizeof(ls));
1243 	if (error != 0)
1244 		return (error);
1245 	B2L_ITIMERVAL(&aitv, &ls);
1246 #ifdef DEBUG
1247 	if (ldebug(setitimer)) {
1248 		printf("setitimer: value: sec: %jd, usec: %ld\n",
1249 		    (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1250 		printf("setitimer: interval: sec: %jd, usec: %ld\n",
1251 		    (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1252 	}
1253 #endif
1254 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
1255 	if (error != 0 || uap->oitv == NULL)
1256 		return (error);
1257 	B2L_ITIMERVAL(&ls, &oitv);
1258 
1259 	return (copyout(&ls, uap->oitv, sizeof(ls)));
1260 }
1261 
1262 int
1263 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1264 {
1265 	int error;
1266 	struct l_itimerval ls;
1267 	struct itimerval aitv;
1268 
1269 #ifdef DEBUG
1270 	if (ldebug(getitimer))
1271 		printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1272 #endif
1273 	error = kern_getitimer(td, uap->which, &aitv);
1274 	if (error != 0)
1275 		return (error);
1276 	B2L_ITIMERVAL(&ls, &aitv);
1277 	return (copyout(&ls, uap->itv, sizeof(ls)));
1278 }
1279 
1280 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1281 int
1282 linux_nice(struct thread *td, struct linux_nice_args *args)
1283 {
1284 	struct setpriority_args bsd_args;
1285 
1286 	bsd_args.which = PRIO_PROCESS;
1287 	bsd_args.who = 0;		/* current process */
1288 	bsd_args.prio = args->inc;
1289 	return (sys_setpriority(td, &bsd_args));
1290 }
1291 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1292 
1293 int
1294 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1295 {
1296 	struct ucred *newcred, *oldcred;
1297 	l_gid_t *linux_gidset;
1298 	gid_t *bsd_gidset;
1299 	int ngrp, error;
1300 	struct proc *p;
1301 
1302 	ngrp = args->gidsetsize;
1303 	if (ngrp < 0 || ngrp >= ngroups_max + 1)
1304 		return (EINVAL);
1305 	linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
1306 	error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1307 	if (error)
1308 		goto out;
1309 	newcred = crget();
1310 	crextend(newcred, ngrp + 1);
1311 	p = td->td_proc;
1312 	PROC_LOCK(p);
1313 	oldcred = p->p_ucred;
1314 	crcopy(newcred, oldcred);
1315 
1316 	/*
1317 	 * cr_groups[0] holds egid. Setting the whole set from
1318 	 * the supplied set will cause egid to be changed too.
1319 	 * Keep cr_groups[0] unchanged to prevent that.
1320 	 */
1321 
1322 	if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
1323 		PROC_UNLOCK(p);
1324 		crfree(newcred);
1325 		goto out;
1326 	}
1327 
1328 	if (ngrp > 0) {
1329 		newcred->cr_ngroups = ngrp + 1;
1330 
1331 		bsd_gidset = newcred->cr_groups;
1332 		ngrp--;
1333 		while (ngrp >= 0) {
1334 			bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1335 			ngrp--;
1336 		}
1337 	} else
1338 		newcred->cr_ngroups = 1;
1339 
1340 	setsugid(p);
1341 	proc_set_cred(p, newcred);
1342 	PROC_UNLOCK(p);
1343 	crfree(oldcred);
1344 	error = 0;
1345 out:
1346 	free(linux_gidset, M_LINUX);
1347 	return (error);
1348 }
1349 
1350 int
1351 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1352 {
1353 	struct ucred *cred;
1354 	l_gid_t *linux_gidset;
1355 	gid_t *bsd_gidset;
1356 	int bsd_gidsetsz, ngrp, error;
1357 
1358 	cred = td->td_ucred;
1359 	bsd_gidset = cred->cr_groups;
1360 	bsd_gidsetsz = cred->cr_ngroups - 1;
1361 
1362 	/*
1363 	 * cr_groups[0] holds egid. Returning the whole set
1364 	 * here will cause a duplicate. Exclude cr_groups[0]
1365 	 * to prevent that.
1366 	 */
1367 
1368 	if ((ngrp = args->gidsetsize) == 0) {
1369 		td->td_retval[0] = bsd_gidsetsz;
1370 		return (0);
1371 	}
1372 
1373 	if (ngrp < bsd_gidsetsz)
1374 		return (EINVAL);
1375 
1376 	ngrp = 0;
1377 	linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1378 	    M_LINUX, M_WAITOK);
1379 	while (ngrp < bsd_gidsetsz) {
1380 		linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1381 		ngrp++;
1382 	}
1383 
1384 	error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1385 	free(linux_gidset, M_LINUX);
1386 	if (error)
1387 		return (error);
1388 
1389 	td->td_retval[0] = ngrp;
1390 	return (0);
1391 }
1392 
1393 int
1394 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1395 {
1396 	struct rlimit bsd_rlim;
1397 	struct l_rlimit rlim;
1398 	u_int which;
1399 	int error;
1400 
1401 #ifdef DEBUG
1402 	if (ldebug(setrlimit))
1403 		printf(ARGS(setrlimit, "%d, %p"),
1404 		    args->resource, (void *)args->rlim);
1405 #endif
1406 
1407 	if (args->resource >= LINUX_RLIM_NLIMITS)
1408 		return (EINVAL);
1409 
1410 	which = linux_to_bsd_resource[args->resource];
1411 	if (which == -1)
1412 		return (EINVAL);
1413 
1414 	error = copyin(args->rlim, &rlim, sizeof(rlim));
1415 	if (error)
1416 		return (error);
1417 
1418 	bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1419 	bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1420 	return (kern_setrlimit(td, which, &bsd_rlim));
1421 }
1422 
1423 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1424 int
1425 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1426 {
1427 	struct l_rlimit rlim;
1428 	struct rlimit bsd_rlim;
1429 	u_int which;
1430 
1431 #ifdef DEBUG
1432 	if (ldebug(old_getrlimit))
1433 		printf(ARGS(old_getrlimit, "%d, %p"),
1434 		    args->resource, (void *)args->rlim);
1435 #endif
1436 
1437 	if (args->resource >= LINUX_RLIM_NLIMITS)
1438 		return (EINVAL);
1439 
1440 	which = linux_to_bsd_resource[args->resource];
1441 	if (which == -1)
1442 		return (EINVAL);
1443 
1444 	lim_rlimit(td, which, &bsd_rlim);
1445 
1446 #ifdef COMPAT_LINUX32
1447 	rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1448 	if (rlim.rlim_cur == UINT_MAX)
1449 		rlim.rlim_cur = INT_MAX;
1450 	rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1451 	if (rlim.rlim_max == UINT_MAX)
1452 		rlim.rlim_max = INT_MAX;
1453 #else
1454 	rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1455 	if (rlim.rlim_cur == ULONG_MAX)
1456 		rlim.rlim_cur = LONG_MAX;
1457 	rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1458 	if (rlim.rlim_max == ULONG_MAX)
1459 		rlim.rlim_max = LONG_MAX;
1460 #endif
1461 	return (copyout(&rlim, args->rlim, sizeof(rlim)));
1462 }
1463 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1464 
1465 int
1466 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1467 {
1468 	struct l_rlimit rlim;
1469 	struct rlimit bsd_rlim;
1470 	u_int which;
1471 
1472 #ifdef DEBUG
1473 	if (ldebug(getrlimit))
1474 		printf(ARGS(getrlimit, "%d, %p"),
1475 		    args->resource, (void *)args->rlim);
1476 #endif
1477 
1478 	if (args->resource >= LINUX_RLIM_NLIMITS)
1479 		return (EINVAL);
1480 
1481 	which = linux_to_bsd_resource[args->resource];
1482 	if (which == -1)
1483 		return (EINVAL);
1484 
1485 	lim_rlimit(td, which, &bsd_rlim);
1486 
1487 	rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1488 	rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1489 	return (copyout(&rlim, args->rlim, sizeof(rlim)));
1490 }
1491 
1492 int
1493 linux_sched_setscheduler(struct thread *td,
1494     struct linux_sched_setscheduler_args *args)
1495 {
1496 	struct sched_param sched_param;
1497 	struct thread *tdt;
1498 	int error, policy;
1499 
1500 #ifdef DEBUG
1501 	if (ldebug(sched_setscheduler))
1502 		printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1503 		    args->pid, args->policy, (const void *)args->param);
1504 #endif
1505 
1506 	switch (args->policy) {
1507 	case LINUX_SCHED_OTHER:
1508 		policy = SCHED_OTHER;
1509 		break;
1510 	case LINUX_SCHED_FIFO:
1511 		policy = SCHED_FIFO;
1512 		break;
1513 	case LINUX_SCHED_RR:
1514 		policy = SCHED_RR;
1515 		break;
1516 	default:
1517 		return (EINVAL);
1518 	}
1519 
1520 	error = copyin(args->param, &sched_param, sizeof(sched_param));
1521 	if (error)
1522 		return (error);
1523 
1524 	tdt = linux_tdfind(td, args->pid, -1);
1525 	if (tdt == NULL)
1526 		return (ESRCH);
1527 
1528 	error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1529 	PROC_UNLOCK(tdt->td_proc);
1530 	return (error);
1531 }
1532 
1533 int
1534 linux_sched_getscheduler(struct thread *td,
1535     struct linux_sched_getscheduler_args *args)
1536 {
1537 	struct thread *tdt;
1538 	int error, policy;
1539 
1540 #ifdef DEBUG
1541 	if (ldebug(sched_getscheduler))
1542 		printf(ARGS(sched_getscheduler, "%d"), args->pid);
1543 #endif
1544 
1545 	tdt = linux_tdfind(td, args->pid, -1);
1546 	if (tdt == NULL)
1547 		return (ESRCH);
1548 
1549 	error = kern_sched_getscheduler(td, tdt, &policy);
1550 	PROC_UNLOCK(tdt->td_proc);
1551 
1552 	switch (policy) {
1553 	case SCHED_OTHER:
1554 		td->td_retval[0] = LINUX_SCHED_OTHER;
1555 		break;
1556 	case SCHED_FIFO:
1557 		td->td_retval[0] = LINUX_SCHED_FIFO;
1558 		break;
1559 	case SCHED_RR:
1560 		td->td_retval[0] = LINUX_SCHED_RR;
1561 		break;
1562 	}
1563 	return (error);
1564 }
1565 
1566 int
1567 linux_sched_get_priority_max(struct thread *td,
1568     struct linux_sched_get_priority_max_args *args)
1569 {
1570 	struct sched_get_priority_max_args bsd;
1571 
1572 #ifdef DEBUG
1573 	if (ldebug(sched_get_priority_max))
1574 		printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1575 #endif
1576 
1577 	switch (args->policy) {
1578 	case LINUX_SCHED_OTHER:
1579 		bsd.policy = SCHED_OTHER;
1580 		break;
1581 	case LINUX_SCHED_FIFO:
1582 		bsd.policy = SCHED_FIFO;
1583 		break;
1584 	case LINUX_SCHED_RR:
1585 		bsd.policy = SCHED_RR;
1586 		break;
1587 	default:
1588 		return (EINVAL);
1589 	}
1590 	return (sys_sched_get_priority_max(td, &bsd));
1591 }
1592 
1593 int
1594 linux_sched_get_priority_min(struct thread *td,
1595     struct linux_sched_get_priority_min_args *args)
1596 {
1597 	struct sched_get_priority_min_args bsd;
1598 
1599 #ifdef DEBUG
1600 	if (ldebug(sched_get_priority_min))
1601 		printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1602 #endif
1603 
1604 	switch (args->policy) {
1605 	case LINUX_SCHED_OTHER:
1606 		bsd.policy = SCHED_OTHER;
1607 		break;
1608 	case LINUX_SCHED_FIFO:
1609 		bsd.policy = SCHED_FIFO;
1610 		break;
1611 	case LINUX_SCHED_RR:
1612 		bsd.policy = SCHED_RR;
1613 		break;
1614 	default:
1615 		return (EINVAL);
1616 	}
1617 	return (sys_sched_get_priority_min(td, &bsd));
1618 }
1619 
1620 #define REBOOT_CAD_ON	0x89abcdef
1621 #define REBOOT_CAD_OFF	0
1622 #define REBOOT_HALT	0xcdef0123
1623 #define REBOOT_RESTART	0x01234567
1624 #define REBOOT_RESTART2	0xA1B2C3D4
1625 #define REBOOT_POWEROFF	0x4321FEDC
1626 #define REBOOT_MAGIC1	0xfee1dead
1627 #define REBOOT_MAGIC2	0x28121969
1628 #define REBOOT_MAGIC2A	0x05121996
1629 #define REBOOT_MAGIC2B	0x16041998
1630 
1631 int
1632 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1633 {
1634 	struct reboot_args bsd_args;
1635 
1636 #ifdef DEBUG
1637 	if (ldebug(reboot))
1638 		printf(ARGS(reboot, "0x%x"), args->cmd);
1639 #endif
1640 
1641 	if (args->magic1 != REBOOT_MAGIC1)
1642 		return (EINVAL);
1643 
1644 	switch (args->magic2) {
1645 	case REBOOT_MAGIC2:
1646 	case REBOOT_MAGIC2A:
1647 	case REBOOT_MAGIC2B:
1648 		break;
1649 	default:
1650 		return (EINVAL);
1651 	}
1652 
1653 	switch (args->cmd) {
1654 	case REBOOT_CAD_ON:
1655 	case REBOOT_CAD_OFF:
1656 		return (priv_check(td, PRIV_REBOOT));
1657 	case REBOOT_HALT:
1658 		bsd_args.opt = RB_HALT;
1659 		break;
1660 	case REBOOT_RESTART:
1661 	case REBOOT_RESTART2:
1662 		bsd_args.opt = 0;
1663 		break;
1664 	case REBOOT_POWEROFF:
1665 		bsd_args.opt = RB_POWEROFF;
1666 		break;
1667 	default:
1668 		return (EINVAL);
1669 	}
1670 	return (sys_reboot(td, &bsd_args));
1671 }
1672 
1673 
1674 /*
1675  * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1676  * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1677  * are assumed to be preserved. The following lightweight syscalls fixes
1678  * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1679  *
1680  * linux_getpid() - MP SAFE
1681  * linux_getgid() - MP SAFE
1682  * linux_getuid() - MP SAFE
1683  */
1684 
1685 int
1686 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1687 {
1688 
1689 #ifdef DEBUG
1690 	if (ldebug(getpid))
1691 		printf(ARGS(getpid, ""));
1692 #endif
1693 	td->td_retval[0] = td->td_proc->p_pid;
1694 
1695 	return (0);
1696 }
1697 
1698 int
1699 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1700 {
1701 	struct linux_emuldata *em;
1702 
1703 #ifdef DEBUG
1704 	if (ldebug(gettid))
1705 		printf(ARGS(gettid, ""));
1706 #endif
1707 
1708 	em = em_find(td);
1709 	KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1710 
1711 	td->td_retval[0] = em->em_tid;
1712 
1713 	return (0);
1714 }
1715 
1716 
1717 int
1718 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1719 {
1720 
1721 #ifdef DEBUG
1722 	if (ldebug(getppid))
1723 		printf(ARGS(getppid, ""));
1724 #endif
1725 
1726 	PROC_LOCK(td->td_proc);
1727 	td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1728 	PROC_UNLOCK(td->td_proc);
1729 	return (0);
1730 }
1731 
1732 int
1733 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1734 {
1735 
1736 #ifdef DEBUG
1737 	if (ldebug(getgid))
1738 		printf(ARGS(getgid, ""));
1739 #endif
1740 
1741 	td->td_retval[0] = td->td_ucred->cr_rgid;
1742 	return (0);
1743 }
1744 
1745 int
1746 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1747 {
1748 
1749 #ifdef DEBUG
1750 	if (ldebug(getuid))
1751 		printf(ARGS(getuid, ""));
1752 #endif
1753 
1754 	td->td_retval[0] = td->td_ucred->cr_ruid;
1755 	return (0);
1756 }
1757 
1758 
1759 int
1760 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1761 {
1762 	struct getsid_args bsd;
1763 
1764 #ifdef DEBUG
1765 	if (ldebug(getsid))
1766 		printf(ARGS(getsid, "%i"), args->pid);
1767 #endif
1768 
1769 	bsd.pid = args->pid;
1770 	return (sys_getsid(td, &bsd));
1771 }
1772 
1773 int
1774 linux_nosys(struct thread *td, struct nosys_args *ignore)
1775 {
1776 
1777 	return (ENOSYS);
1778 }
1779 
1780 int
1781 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1782 {
1783 	struct getpriority_args bsd_args;
1784 	int error;
1785 
1786 #ifdef DEBUG
1787 	if (ldebug(getpriority))
1788 		printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1789 #endif
1790 
1791 	bsd_args.which = args->which;
1792 	bsd_args.who = args->who;
1793 	error = sys_getpriority(td, &bsd_args);
1794 	td->td_retval[0] = 20 - td->td_retval[0];
1795 	return (error);
1796 }
1797 
1798 int
1799 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1800 {
1801 	int name[2];
1802 
1803 #ifdef DEBUG
1804 	if (ldebug(sethostname))
1805 		printf(ARGS(sethostname, "*, %i"), args->len);
1806 #endif
1807 
1808 	name[0] = CTL_KERN;
1809 	name[1] = KERN_HOSTNAME;
1810 	return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1811 	    args->len, 0, 0));
1812 }
1813 
1814 int
1815 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1816 {
1817 	int name[2];
1818 
1819 #ifdef DEBUG
1820 	if (ldebug(setdomainname))
1821 		printf(ARGS(setdomainname, "*, %i"), args->len);
1822 #endif
1823 
1824 	name[0] = CTL_KERN;
1825 	name[1] = KERN_NISDOMAINNAME;
1826 	return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1827 	    args->len, 0, 0));
1828 }
1829 
1830 int
1831 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1832 {
1833 
1834 #ifdef DEBUG
1835 	if (ldebug(exit_group))
1836 		printf(ARGS(exit_group, "%i"), args->error_code);
1837 #endif
1838 
1839 	LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1840 	    args->error_code);
1841 
1842 	/*
1843 	 * XXX: we should send a signal to the parent if
1844 	 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1845 	 * as it doesnt occur often.
1846 	 */
1847 	exit1(td, args->error_code, 0);
1848 		/* NOTREACHED */
1849 }
1850 
1851 #define _LINUX_CAPABILITY_VERSION  0x19980330
1852 
1853 struct l_user_cap_header {
1854 	l_int	version;
1855 	l_int	pid;
1856 };
1857 
1858 struct l_user_cap_data {
1859 	l_int	effective;
1860 	l_int	permitted;
1861 	l_int	inheritable;
1862 };
1863 
1864 int
1865 linux_capget(struct thread *td, struct linux_capget_args *args)
1866 {
1867 	struct l_user_cap_header luch;
1868 	struct l_user_cap_data lucd;
1869 	int error;
1870 
1871 	if (args->hdrp == NULL)
1872 		return (EFAULT);
1873 
1874 	error = copyin(args->hdrp, &luch, sizeof(luch));
1875 	if (error != 0)
1876 		return (error);
1877 
1878 	if (luch.version != _LINUX_CAPABILITY_VERSION) {
1879 		luch.version = _LINUX_CAPABILITY_VERSION;
1880 		error = copyout(&luch, args->hdrp, sizeof(luch));
1881 		if (error)
1882 			return (error);
1883 		return (EINVAL);
1884 	}
1885 
1886 	if (luch.pid)
1887 		return (EPERM);
1888 
1889 	if (args->datap) {
1890 		/*
1891 		 * The current implementation doesn't support setting
1892 		 * a capability (it's essentially a stub) so indicate
1893 		 * that no capabilities are currently set or available
1894 		 * to request.
1895 		 */
1896 		bzero (&lucd, sizeof(lucd));
1897 		error = copyout(&lucd, args->datap, sizeof(lucd));
1898 	}
1899 
1900 	return (error);
1901 }
1902 
1903 int
1904 linux_capset(struct thread *td, struct linux_capset_args *args)
1905 {
1906 	struct l_user_cap_header luch;
1907 	struct l_user_cap_data lucd;
1908 	int error;
1909 
1910 	if (args->hdrp == NULL || args->datap == NULL)
1911 		return (EFAULT);
1912 
1913 	error = copyin(args->hdrp, &luch, sizeof(luch));
1914 	if (error != 0)
1915 		return (error);
1916 
1917 	if (luch.version != _LINUX_CAPABILITY_VERSION) {
1918 		luch.version = _LINUX_CAPABILITY_VERSION;
1919 		error = copyout(&luch, args->hdrp, sizeof(luch));
1920 		if (error)
1921 			return (error);
1922 		return (EINVAL);
1923 	}
1924 
1925 	if (luch.pid)
1926 		return (EPERM);
1927 
1928 	error = copyin(args->datap, &lucd, sizeof(lucd));
1929 	if (error != 0)
1930 		return (error);
1931 
1932 	/* We currently don't support setting any capabilities. */
1933 	if (lucd.effective || lucd.permitted || lucd.inheritable) {
1934 		linux_msg(td,
1935 			  "capset effective=0x%x, permitted=0x%x, "
1936 			  "inheritable=0x%x is not implemented",
1937 			  (int)lucd.effective, (int)lucd.permitted,
1938 			  (int)lucd.inheritable);
1939 		return (EPERM);
1940 	}
1941 
1942 	return (0);
1943 }
1944 
1945 int
1946 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1947 {
1948 	int error = 0, max_size;
1949 	struct proc *p = td->td_proc;
1950 	char comm[LINUX_MAX_COMM_LEN];
1951 	struct linux_emuldata *em;
1952 	int pdeath_signal;
1953 
1954 #ifdef DEBUG
1955 	if (ldebug(prctl))
1956 		printf(ARGS(prctl, "%d, %ju, %ju, %ju, %ju"), args->option,
1957 		    (uintmax_t)args->arg2, (uintmax_t)args->arg3,
1958 		    (uintmax_t)args->arg4, (uintmax_t)args->arg5);
1959 #endif
1960 
1961 	switch (args->option) {
1962 	case LINUX_PR_SET_PDEATHSIG:
1963 		if (!LINUX_SIG_VALID(args->arg2))
1964 			return (EINVAL);
1965 		em = em_find(td);
1966 		KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1967 		em->pdeath_signal = args->arg2;
1968 		break;
1969 	case LINUX_PR_GET_PDEATHSIG:
1970 		em = em_find(td);
1971 		KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1972 		pdeath_signal = em->pdeath_signal;
1973 		error = copyout(&pdeath_signal,
1974 		    (void *)(register_t)args->arg2,
1975 		    sizeof(pdeath_signal));
1976 		break;
1977 	case LINUX_PR_GET_KEEPCAPS:
1978 		/*
1979 		 * Indicate that we always clear the effective and
1980 		 * permitted capability sets when the user id becomes
1981 		 * non-zero (actually the capability sets are simply
1982 		 * always zero in the current implementation).
1983 		 */
1984 		td->td_retval[0] = 0;
1985 		break;
1986 	case LINUX_PR_SET_KEEPCAPS:
1987 		/*
1988 		 * Ignore requests to keep the effective and permitted
1989 		 * capability sets when the user id becomes non-zero.
1990 		 */
1991 		break;
1992 	case LINUX_PR_SET_NAME:
1993 		/*
1994 		 * To be on the safe side we need to make sure to not
1995 		 * overflow the size a linux program expects. We already
1996 		 * do this here in the copyin, so that we don't need to
1997 		 * check on copyout.
1998 		 */
1999 		max_size = MIN(sizeof(comm), sizeof(p->p_comm));
2000 		error = copyinstr((void *)(register_t)args->arg2, comm,
2001 		    max_size, NULL);
2002 
2003 		/* Linux silently truncates the name if it is too long. */
2004 		if (error == ENAMETOOLONG) {
2005 			/*
2006 			 * XXX: copyinstr() isn't documented to populate the
2007 			 * array completely, so do a copyin() to be on the
2008 			 * safe side. This should be changed in case
2009 			 * copyinstr() is changed to guarantee this.
2010 			 */
2011 			error = copyin((void *)(register_t)args->arg2, comm,
2012 			    max_size - 1);
2013 			comm[max_size - 1] = '\0';
2014 		}
2015 		if (error)
2016 			return (error);
2017 
2018 		PROC_LOCK(p);
2019 		strlcpy(p->p_comm, comm, sizeof(p->p_comm));
2020 		PROC_UNLOCK(p);
2021 		break;
2022 	case LINUX_PR_GET_NAME:
2023 		PROC_LOCK(p);
2024 		strlcpy(comm, p->p_comm, sizeof(comm));
2025 		PROC_UNLOCK(p);
2026 		error = copyout(comm, (void *)(register_t)args->arg2,
2027 		    strlen(comm) + 1);
2028 		break;
2029 	default:
2030 		error = EINVAL;
2031 		break;
2032 	}
2033 
2034 	return (error);
2035 }
2036 
2037 int
2038 linux_sched_setparam(struct thread *td,
2039     struct linux_sched_setparam_args *uap)
2040 {
2041 	struct sched_param sched_param;
2042 	struct thread *tdt;
2043 	int error;
2044 
2045 #ifdef DEBUG
2046 	if (ldebug(sched_setparam))
2047 		printf(ARGS(sched_setparam, "%d, *"), uap->pid);
2048 #endif
2049 
2050 	error = copyin(uap->param, &sched_param, sizeof(sched_param));
2051 	if (error)
2052 		return (error);
2053 
2054 	tdt = linux_tdfind(td, uap->pid, -1);
2055 	if (tdt == NULL)
2056 		return (ESRCH);
2057 
2058 	error = kern_sched_setparam(td, tdt, &sched_param);
2059 	PROC_UNLOCK(tdt->td_proc);
2060 	return (error);
2061 }
2062 
2063 int
2064 linux_sched_getparam(struct thread *td,
2065     struct linux_sched_getparam_args *uap)
2066 {
2067 	struct sched_param sched_param;
2068 	struct thread *tdt;
2069 	int error;
2070 
2071 #ifdef DEBUG
2072 	if (ldebug(sched_getparam))
2073 		printf(ARGS(sched_getparam, "%d, *"), uap->pid);
2074 #endif
2075 
2076 	tdt = linux_tdfind(td, uap->pid, -1);
2077 	if (tdt == NULL)
2078 		return (ESRCH);
2079 
2080 	error = kern_sched_getparam(td, tdt, &sched_param);
2081 	PROC_UNLOCK(tdt->td_proc);
2082 	if (error == 0)
2083 		error = copyout(&sched_param, uap->param,
2084 		    sizeof(sched_param));
2085 	return (error);
2086 }
2087 
2088 /*
2089  * Get affinity of a process.
2090  */
2091 int
2092 linux_sched_getaffinity(struct thread *td,
2093     struct linux_sched_getaffinity_args *args)
2094 {
2095 	int error;
2096 	struct thread *tdt;
2097 	struct cpuset_getaffinity_args cga;
2098 
2099 #ifdef DEBUG
2100 	if (ldebug(sched_getaffinity))
2101 		printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
2102 		    args->len);
2103 #endif
2104 	if (args->len < sizeof(cpuset_t))
2105 		return (EINVAL);
2106 
2107 	tdt = linux_tdfind(td, args->pid, -1);
2108 	if (tdt == NULL)
2109 		return (ESRCH);
2110 
2111 	PROC_UNLOCK(tdt->td_proc);
2112 	cga.level = CPU_LEVEL_WHICH;
2113 	cga.which = CPU_WHICH_TID;
2114 	cga.id = tdt->td_tid;
2115 	cga.cpusetsize = sizeof(cpuset_t);
2116 	cga.mask = (cpuset_t *) args->user_mask_ptr;
2117 
2118 	if ((error = sys_cpuset_getaffinity(td, &cga)) == 0)
2119 		td->td_retval[0] = sizeof(cpuset_t);
2120 
2121 	return (error);
2122 }
2123 
2124 /*
2125  *  Set affinity of a process.
2126  */
2127 int
2128 linux_sched_setaffinity(struct thread *td,
2129     struct linux_sched_setaffinity_args *args)
2130 {
2131 	struct cpuset_setaffinity_args csa;
2132 	struct thread *tdt;
2133 
2134 #ifdef DEBUG
2135 	if (ldebug(sched_setaffinity))
2136 		printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
2137 		    args->len);
2138 #endif
2139 	if (args->len < sizeof(cpuset_t))
2140 		return (EINVAL);
2141 
2142 	tdt = linux_tdfind(td, args->pid, -1);
2143 	if (tdt == NULL)
2144 		return (ESRCH);
2145 
2146 	PROC_UNLOCK(tdt->td_proc);
2147 	csa.level = CPU_LEVEL_WHICH;
2148 	csa.which = CPU_WHICH_TID;
2149 	csa.id = tdt->td_tid;
2150 	csa.cpusetsize = sizeof(cpuset_t);
2151 	csa.mask = (cpuset_t *) args->user_mask_ptr;
2152 
2153 	return (sys_cpuset_setaffinity(td, &csa));
2154 }
2155 
2156 struct linux_rlimit64 {
2157 	uint64_t	rlim_cur;
2158 	uint64_t	rlim_max;
2159 };
2160 
2161 int
2162 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
2163 {
2164 	struct rlimit rlim, nrlim;
2165 	struct linux_rlimit64 lrlim;
2166 	struct proc *p;
2167 	u_int which;
2168 	int flags;
2169 	int error;
2170 
2171 #ifdef DEBUG
2172 	if (ldebug(prlimit64))
2173 		printf(ARGS(prlimit64, "%d, %d, %p, %p"), args->pid,
2174 		    args->resource, (void *)args->new, (void *)args->old);
2175 #endif
2176 
2177 	if (args->resource >= LINUX_RLIM_NLIMITS)
2178 		return (EINVAL);
2179 
2180 	which = linux_to_bsd_resource[args->resource];
2181 	if (which == -1)
2182 		return (EINVAL);
2183 
2184 	if (args->new != NULL) {
2185 		/*
2186 		 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
2187 		 * rlim is unsigned 64-bit. FreeBSD treats negative limits
2188 		 * as INFINITY so we do not need a conversion even.
2189 		 */
2190 		error = copyin(args->new, &nrlim, sizeof(nrlim));
2191 		if (error != 0)
2192 			return (error);
2193 	}
2194 
2195 	flags = PGET_HOLD | PGET_NOTWEXIT;
2196 	if (args->new != NULL)
2197 		flags |= PGET_CANDEBUG;
2198 	else
2199 		flags |= PGET_CANSEE;
2200 	error = pget(args->pid, flags, &p);
2201 	if (error != 0)
2202 		return (error);
2203 
2204 	if (args->old != NULL) {
2205 		PROC_LOCK(p);
2206 		lim_rlimit_proc(p, which, &rlim);
2207 		PROC_UNLOCK(p);
2208 		if (rlim.rlim_cur == RLIM_INFINITY)
2209 			lrlim.rlim_cur = LINUX_RLIM_INFINITY;
2210 		else
2211 			lrlim.rlim_cur = rlim.rlim_cur;
2212 		if (rlim.rlim_max == RLIM_INFINITY)
2213 			lrlim.rlim_max = LINUX_RLIM_INFINITY;
2214 		else
2215 			lrlim.rlim_max = rlim.rlim_max;
2216 		error = copyout(&lrlim, args->old, sizeof(lrlim));
2217 		if (error != 0)
2218 			goto out;
2219 	}
2220 
2221 	if (args->new != NULL)
2222 		error = kern_proc_setrlimit(td, p, which, &nrlim);
2223 
2224  out:
2225 	PRELE(p);
2226 	return (error);
2227 }
2228 
2229 int
2230 linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2231 {
2232 	struct timeval utv, tv0, tv1, *tvp;
2233 	struct l_pselect6arg lpse6;
2234 	struct l_timespec lts;
2235 	struct timespec uts;
2236 	l_sigset_t l_ss;
2237 	sigset_t *ssp;
2238 	sigset_t ss;
2239 	int error;
2240 
2241 	ssp = NULL;
2242 	if (args->sig != NULL) {
2243 		error = copyin(args->sig, &lpse6, sizeof(lpse6));
2244 		if (error != 0)
2245 			return (error);
2246 		if (lpse6.ss_len != sizeof(l_ss))
2247 			return (EINVAL);
2248 		if (lpse6.ss != 0) {
2249 			error = copyin(PTRIN(lpse6.ss), &l_ss,
2250 			    sizeof(l_ss));
2251 			if (error != 0)
2252 				return (error);
2253 			linux_to_bsd_sigset(&l_ss, &ss);
2254 			ssp = &ss;
2255 		}
2256 	}
2257 
2258 	/*
2259 	 * Currently glibc changes nanosecond number to microsecond.
2260 	 * This mean losing precision but for now it is hardly seen.
2261 	 */
2262 	if (args->tsp != NULL) {
2263 		error = copyin(args->tsp, &lts, sizeof(lts));
2264 		if (error != 0)
2265 			return (error);
2266 		error = linux_to_native_timespec(&uts, &lts);
2267 		if (error != 0)
2268 			return (error);
2269 
2270 		TIMESPEC_TO_TIMEVAL(&utv, &uts);
2271 		if (itimerfix(&utv))
2272 			return (EINVAL);
2273 
2274 		microtime(&tv0);
2275 		tvp = &utv;
2276 	} else
2277 		tvp = NULL;
2278 
2279 	error = kern_pselect(td, args->nfds, args->readfds, args->writefds,
2280 	    args->exceptfds, tvp, ssp, LINUX_NFDBITS);
2281 
2282 	if (error == 0 && args->tsp != NULL) {
2283 		if (td->td_retval[0] != 0) {
2284 			/*
2285 			 * Compute how much time was left of the timeout,
2286 			 * by subtracting the current time and the time
2287 			 * before we started the call, and subtracting
2288 			 * that result from the user-supplied value.
2289 			 */
2290 
2291 			microtime(&tv1);
2292 			timevalsub(&tv1, &tv0);
2293 			timevalsub(&utv, &tv1);
2294 			if (utv.tv_sec < 0)
2295 				timevalclear(&utv);
2296 		} else
2297 			timevalclear(&utv);
2298 
2299 		TIMEVAL_TO_TIMESPEC(&utv, &uts);
2300 
2301 		native_to_linux_timespec(&lts, &uts);
2302 		error = copyout(&lts, args->tsp, sizeof(lts));
2303 	}
2304 
2305 	return (error);
2306 }
2307 
2308 int
2309 linux_ppoll(struct thread *td, struct linux_ppoll_args *args)
2310 {
2311 	struct timespec ts0, ts1;
2312 	struct l_timespec lts;
2313 	struct timespec uts, *tsp;
2314 	l_sigset_t l_ss;
2315 	sigset_t *ssp;
2316 	sigset_t ss;
2317 	int error;
2318 
2319 	if (args->sset != NULL) {
2320 		if (args->ssize != sizeof(l_ss))
2321 			return (EINVAL);
2322 		error = copyin(args->sset, &l_ss, sizeof(l_ss));
2323 		if (error)
2324 			return (error);
2325 		linux_to_bsd_sigset(&l_ss, &ss);
2326 		ssp = &ss;
2327 	} else
2328 		ssp = NULL;
2329 	if (args->tsp != NULL) {
2330 		error = copyin(args->tsp, &lts, sizeof(lts));
2331 		if (error)
2332 			return (error);
2333 		error = linux_to_native_timespec(&uts, &lts);
2334 		if (error != 0)
2335 			return (error);
2336 
2337 		nanotime(&ts0);
2338 		tsp = &uts;
2339 	} else
2340 		tsp = NULL;
2341 
2342 	error = kern_poll(td, args->fds, args->nfds, tsp, ssp);
2343 
2344 	if (error == 0 && args->tsp != NULL) {
2345 		if (td->td_retval[0]) {
2346 			nanotime(&ts1);
2347 			timespecsub(&ts1, &ts0);
2348 			timespecsub(&uts, &ts1);
2349 			if (uts.tv_sec < 0)
2350 				timespecclear(&uts);
2351 		} else
2352 			timespecclear(&uts);
2353 
2354 		native_to_linux_timespec(&lts, &uts);
2355 		error = copyout(&lts, args->tsp, sizeof(lts));
2356 	}
2357 
2358 	return (error);
2359 }
2360 
2361 #if defined(DEBUG) || defined(KTR)
2362 /* XXX: can be removed when every ldebug(...) and KTR stuff are removed. */
2363 
2364 #ifdef COMPAT_LINUX32
2365 #define	L_MAXSYSCALL	LINUX32_SYS_MAXSYSCALL
2366 #else
2367 #define	L_MAXSYSCALL	LINUX_SYS_MAXSYSCALL
2368 #endif
2369 
2370 u_char linux_debug_map[howmany(L_MAXSYSCALL, sizeof(u_char))];
2371 
2372 static int
2373 linux_debug(int syscall, int toggle, int global)
2374 {
2375 
2376 	if (global) {
2377 		char c = toggle ? 0 : 0xff;
2378 
2379 		memset(linux_debug_map, c, sizeof(linux_debug_map));
2380 		return (0);
2381 	}
2382 	if (syscall < 0 || syscall >= L_MAXSYSCALL)
2383 		return (EINVAL);
2384 	if (toggle)
2385 		clrbit(linux_debug_map, syscall);
2386 	else
2387 		setbit(linux_debug_map, syscall);
2388 	return (0);
2389 }
2390 #undef L_MAXSYSCALL
2391 
2392 /*
2393  * Usage: sysctl linux.debug=<syscall_nr>.<0/1>
2394  *
2395  *    E.g.: sysctl linux.debug=21.0
2396  *
2397  * As a special case, syscall "all" will apply to all syscalls globally.
2398  */
2399 #define LINUX_MAX_DEBUGSTR	16
2400 int
2401 linux_sysctl_debug(SYSCTL_HANDLER_ARGS)
2402 {
2403 	char value[LINUX_MAX_DEBUGSTR], *p;
2404 	int error, sysc, toggle;
2405 	int global = 0;
2406 
2407 	value[0] = '\0';
2408 	error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req);
2409 	if (error || req->newptr == NULL)
2410 		return (error);
2411 	for (p = value; *p != '\0' && *p != '.'; p++);
2412 	if (*p == '\0')
2413 		return (EINVAL);
2414 	*p++ = '\0';
2415 	sysc = strtol(value, NULL, 0);
2416 	toggle = strtol(p, NULL, 0);
2417 	if (strcmp(value, "all") == 0)
2418 		global = 1;
2419 	error = linux_debug(sysc, toggle, global);
2420 	return (error);
2421 }
2422 
2423 #endif /* DEBUG || KTR */
2424 
2425 int
2426 linux_sched_rr_get_interval(struct thread *td,
2427     struct linux_sched_rr_get_interval_args *uap)
2428 {
2429 	struct timespec ts;
2430 	struct l_timespec lts;
2431 	struct thread *tdt;
2432 	int error;
2433 
2434 	/*
2435 	 * According to man in case the invalid pid specified
2436 	 * EINVAL should be returned.
2437 	 */
2438 	if (uap->pid < 0)
2439 		return (EINVAL);
2440 
2441 	tdt = linux_tdfind(td, uap->pid, -1);
2442 	if (tdt == NULL)
2443 		return (ESRCH);
2444 
2445 	error = kern_sched_rr_get_interval_td(td, tdt, &ts);
2446 	PROC_UNLOCK(tdt->td_proc);
2447 	if (error != 0)
2448 		return (error);
2449 	native_to_linux_timespec(&lts, &ts);
2450 	return (copyout(&lts, uap->interval, sizeof(lts)));
2451 }
2452 
2453 /*
2454  * In case when the Linux thread is the initial thread in
2455  * the thread group thread id is equal to the process id.
2456  * Glibc depends on this magic (assert in pthread_getattr_np.c).
2457  */
2458 struct thread *
2459 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
2460 {
2461 	struct linux_emuldata *em;
2462 	struct thread *tdt;
2463 	struct proc *p;
2464 
2465 	tdt = NULL;
2466 	if (tid == 0 || tid == td->td_tid) {
2467 		tdt = td;
2468 		PROC_LOCK(tdt->td_proc);
2469 	} else if (tid > PID_MAX)
2470 		tdt = tdfind(tid, pid);
2471 	else {
2472 		/*
2473 		 * Initial thread where the tid equal to the pid.
2474 		 */
2475 		p = pfind(tid);
2476 		if (p != NULL) {
2477 			if (SV_PROC_ABI(p) != SV_ABI_LINUX) {
2478 				/*
2479 				 * p is not a Linuxulator process.
2480 				 */
2481 				PROC_UNLOCK(p);
2482 				return (NULL);
2483 			}
2484 			FOREACH_THREAD_IN_PROC(p, tdt) {
2485 				em = em_find(tdt);
2486 				if (tid == em->em_tid)
2487 					return (tdt);
2488 			}
2489 			PROC_UNLOCK(p);
2490 		}
2491 		return (NULL);
2492 	}
2493 
2494 	return (tdt);
2495 }
2496 
2497 void
2498 linux_to_bsd_waitopts(int options, int *bsdopts)
2499 {
2500 
2501 	if (options & LINUX_WNOHANG)
2502 		*bsdopts |= WNOHANG;
2503 	if (options & LINUX_WUNTRACED)
2504 		*bsdopts |= WUNTRACED;
2505 	if (options & LINUX_WEXITED)
2506 		*bsdopts |= WEXITED;
2507 	if (options & LINUX_WCONTINUED)
2508 		*bsdopts |= WCONTINUED;
2509 	if (options & LINUX_WNOWAIT)
2510 		*bsdopts |= WNOWAIT;
2511 
2512 	if (options & __WCLONE)
2513 		*bsdopts |= WLINUXCLONE;
2514 }
2515