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