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