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