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