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