xref: /freebsd/sys/compat/linux/linux_misc.c (revision c0020399a650364d0134f79f3fa319f84064372d)
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 #include "opt_mac.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/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 #include <sys/vimage.h>
68 
69 #include <security/mac/mac_framework.h>
70 
71 #include <vm/vm.h>
72 #include <vm/pmap.h>
73 #include <vm/vm_kern.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_extern.h>
76 #include <vm/vm_object.h>
77 #include <vm/swap_pager.h>
78 
79 #ifdef COMPAT_LINUX32
80 #include <machine/../linux32/linux.h>
81 #include <machine/../linux32/linux32_proto.h>
82 #else
83 #include <machine/../linux/linux.h>
84 #include <machine/../linux/linux_proto.h>
85 #endif
86 
87 #include <compat/linux/linux_file.h>
88 #include <compat/linux/linux_mib.h>
89 #include <compat/linux/linux_signal.h>
90 #include <compat/linux/linux_util.h>
91 #include <compat/linux/linux_sysproto.h>
92 #include <compat/linux/linux_emul.h>
93 #include <compat/linux/linux_misc.h>
94 
95 #define BSD_TO_LINUX_SIGNAL(sig)	\
96 	(((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
97 
98 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
99 	RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
100 	RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
101 	RLIMIT_MEMLOCK, RLIMIT_AS
102 };
103 
104 struct l_sysinfo {
105 	l_long		uptime;		/* Seconds since boot */
106 	l_ulong		loads[3];	/* 1, 5, and 15 minute load averages */
107 #define LINUX_SYSINFO_LOADS_SCALE 65536
108 	l_ulong		totalram;	/* Total usable main memory size */
109 	l_ulong		freeram;	/* Available memory size */
110 	l_ulong		sharedram;	/* Amount of shared memory */
111 	l_ulong		bufferram;	/* Memory used by buffers */
112 	l_ulong		totalswap;	/* Total swap space size */
113 	l_ulong		freeswap;	/* swap space still available */
114 	l_ushort	procs;		/* Number of current processes */
115 	l_ushort	pads;
116 	l_ulong		totalbig;
117 	l_ulong		freebig;
118 	l_uint		mem_unit;
119 	char		_f[20-2*sizeof(l_long)-sizeof(l_int)];	/* padding */
120 };
121 int
122 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
123 {
124 	struct l_sysinfo sysinfo;
125 	vm_object_t object;
126 	int i, j;
127 	struct timespec ts;
128 
129 	getnanouptime(&ts);
130 	if (ts.tv_nsec != 0)
131 		ts.tv_sec++;
132 	sysinfo.uptime = ts.tv_sec;
133 
134 	/* Use the information from the mib to get our load averages */
135 	for (i = 0; i < 3; i++)
136 		sysinfo.loads[i] = averunnable.ldavg[i] *
137 		    LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
138 
139 	sysinfo.totalram = physmem * PAGE_SIZE;
140 	sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
141 
142 	sysinfo.sharedram = 0;
143 	mtx_lock(&vm_object_list_mtx);
144 	TAILQ_FOREACH(object, &vm_object_list, object_list)
145 		if (object->shadow_count > 1)
146 			sysinfo.sharedram += object->resident_page_count;
147 	mtx_unlock(&vm_object_list_mtx);
148 
149 	sysinfo.sharedram *= PAGE_SIZE;
150 	sysinfo.bufferram = 0;
151 
152 	swap_pager_status(&i, &j);
153 	sysinfo.totalswap = i * PAGE_SIZE;
154 	sysinfo.freeswap = (i - j) * PAGE_SIZE;
155 
156 	sysinfo.procs = nprocs;
157 
158 	/* The following are only present in newer Linux kernels. */
159 	sysinfo.totalbig = 0;
160 	sysinfo.freebig = 0;
161 	sysinfo.mem_unit = 1;
162 
163 	return copyout(&sysinfo, args->info, sizeof(sysinfo));
164 }
165 
166 int
167 linux_alarm(struct thread *td, struct linux_alarm_args *args)
168 {
169 	struct itimerval it, old_it;
170 	u_int secs;
171 	int error;
172 
173 #ifdef DEBUG
174 	if (ldebug(alarm))
175 		printf(ARGS(alarm, "%u"), args->secs);
176 #endif
177 
178 	secs = args->secs;
179 
180 	if (secs > INT_MAX)
181 		secs = INT_MAX;
182 
183 	it.it_value.tv_sec = (long) secs;
184 	it.it_value.tv_usec = 0;
185 	it.it_interval.tv_sec = 0;
186 	it.it_interval.tv_usec = 0;
187 	error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
188 	if (error)
189 		return (error);
190 	if (timevalisset(&old_it.it_value)) {
191 		if (old_it.it_value.tv_usec != 0)
192 			old_it.it_value.tv_sec++;
193 		td->td_retval[0] = old_it.it_value.tv_sec;
194 	}
195 	return (0);
196 }
197 
198 int
199 linux_brk(struct thread *td, struct linux_brk_args *args)
200 {
201 	struct vmspace *vm = td->td_proc->p_vmspace;
202 	vm_offset_t new, old;
203 	struct obreak_args /* {
204 		char * nsize;
205 	} */ tmp;
206 
207 #ifdef DEBUG
208 	if (ldebug(brk))
209 		printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
210 #endif
211 	old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
212 	new = (vm_offset_t)args->dsend;
213 	tmp.nsize = (char *)new;
214 	if (((caddr_t)new > vm->vm_daddr) && !obreak(td, &tmp))
215 		td->td_retval[0] = (long)new;
216 	else
217 		td->td_retval[0] = (long)old;
218 
219 	return 0;
220 }
221 
222 #if defined(__i386__)
223 /* XXX: what about amd64/linux32? */
224 
225 int
226 linux_uselib(struct thread *td, struct linux_uselib_args *args)
227 {
228 	struct nameidata ni;
229 	struct vnode *vp;
230 	struct exec *a_out;
231 	struct vattr attr;
232 	vm_offset_t vmaddr;
233 	unsigned long file_offset;
234 	vm_offset_t buffer;
235 	unsigned long bss_size;
236 	char *library;
237 	int error;
238 	int locked, vfslocked;
239 
240 	LCONVPATHEXIST(td, args->library, &library);
241 
242 #ifdef DEBUG
243 	if (ldebug(uselib))
244 		printf(ARGS(uselib, "%s"), library);
245 #endif
246 
247 	a_out = NULL;
248 	vfslocked = 0;
249 	locked = 0;
250 	vp = NULL;
251 
252 	NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
253 	    UIO_SYSSPACE, library, td);
254 	error = namei(&ni);
255 	LFREEPATH(library);
256 	if (error)
257 		goto cleanup;
258 
259 	vp = ni.ni_vp;
260 	vfslocked = NDHASGIANT(&ni);
261 	NDFREE(&ni, NDF_ONLY_PNBUF);
262 
263 	/*
264 	 * From here on down, we have a locked vnode that must be unlocked.
265 	 * XXX: The code below largely duplicates exec_check_permissions().
266 	 */
267 	locked = 1;
268 
269 	/* Writable? */
270 	if (vp->v_writecount) {
271 		error = ETXTBSY;
272 		goto cleanup;
273 	}
274 
275 	/* Executable? */
276 	error = VOP_GETATTR(vp, &attr, td->td_ucred);
277 	if (error)
278 		goto cleanup;
279 
280 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
281 	    ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
282 		/* EACCESS is what exec(2) returns. */
283 		error = ENOEXEC;
284 		goto cleanup;
285 	}
286 
287 	/* Sensible size? */
288 	if (attr.va_size == 0) {
289 		error = ENOEXEC;
290 		goto cleanup;
291 	}
292 
293 	/* Can we access it? */
294 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
295 	if (error)
296 		goto cleanup;
297 
298 	/*
299 	 * XXX: This should use vn_open() so that it is properly authorized,
300 	 * and to reduce code redundancy all over the place here.
301 	 * XXX: Not really, it duplicates far more of exec_check_permissions()
302 	 * than vn_open().
303 	 */
304 #ifdef MAC
305 	error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
306 	if (error)
307 		goto cleanup;
308 #endif
309 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
310 	if (error)
311 		goto cleanup;
312 
313 	/* Pull in executable header into kernel_map */
314 	error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
315 	    VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
316 	if (error)
317 		goto cleanup;
318 
319 	/* Is it a Linux binary ? */
320 	if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
321 		error = ENOEXEC;
322 		goto cleanup;
323 	}
324 
325 	/*
326 	 * While we are here, we should REALLY do some more checks
327 	 */
328 
329 	/* Set file/virtual offset based on a.out variant. */
330 	switch ((int)(a_out->a_magic & 0xffff)) {
331 	case 0413:			/* ZMAGIC */
332 		file_offset = 1024;
333 		break;
334 	case 0314:			/* QMAGIC */
335 		file_offset = 0;
336 		break;
337 	default:
338 		error = ENOEXEC;
339 		goto cleanup;
340 	}
341 
342 	bss_size = round_page(a_out->a_bss);
343 
344 	/* Check various fields in header for validity/bounds. */
345 	if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
346 		error = ENOEXEC;
347 		goto cleanup;
348 	}
349 
350 	/* text + data can't exceed file size */
351 	if (a_out->a_data + a_out->a_text > attr.va_size) {
352 		error = EFAULT;
353 		goto cleanup;
354 	}
355 
356 	/*
357 	 * text/data/bss must not exceed limits
358 	 * XXX - this is not complete. it should check current usage PLUS
359 	 * the resources needed by this library.
360 	 */
361 	PROC_LOCK(td->td_proc);
362 	if (a_out->a_text > maxtsiz ||
363 	    a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA)) {
364 		PROC_UNLOCK(td->td_proc);
365 		error = ENOMEM;
366 		goto cleanup;
367 	}
368 	PROC_UNLOCK(td->td_proc);
369 
370 	/*
371 	 * Prevent more writers.
372 	 * XXX: Note that if any of the VM operations fail below we don't
373 	 * clear this flag.
374 	 */
375 	vp->v_vflag |= VV_TEXT;
376 
377 	/*
378 	 * Lock no longer needed
379 	 */
380 	locked = 0;
381 	VOP_UNLOCK(vp, 0);
382 	VFS_UNLOCK_GIANT(vfslocked);
383 
384 	/*
385 	 * Check if file_offset page aligned. Currently we cannot handle
386 	 * misalinged file offsets, and so we read in the entire image
387 	 * (what a waste).
388 	 */
389 	if (file_offset & PAGE_MASK) {
390 #ifdef DEBUG
391 		printf("uselib: Non page aligned binary %lu\n", file_offset);
392 #endif
393 		/* Map text+data read/write/execute */
394 
395 		/* a_entry is the load address and is page aligned */
396 		vmaddr = trunc_page(a_out->a_entry);
397 
398 		/* get anon user mapping, read+write+execute */
399 		error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
400 		    &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
401 		    VM_PROT_ALL, 0);
402 		if (error)
403 			goto cleanup;
404 
405 		/* map file into kernel_map */
406 		error = vm_mmap(kernel_map, &buffer,
407 		    round_page(a_out->a_text + a_out->a_data + file_offset),
408 		    VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp,
409 		    trunc_page(file_offset));
410 		if (error)
411 			goto cleanup;
412 
413 		/* copy from kernel VM space to user space */
414 		error = copyout(PTRIN(buffer + file_offset),
415 		    (void *)vmaddr, a_out->a_text + a_out->a_data);
416 
417 		/* release temporary kernel space */
418 		vm_map_remove(kernel_map, buffer, buffer +
419 		    round_page(a_out->a_text + a_out->a_data + file_offset));
420 
421 		if (error)
422 			goto cleanup;
423 	} else {
424 #ifdef DEBUG
425 		printf("uselib: Page aligned binary %lu\n", file_offset);
426 #endif
427 		/*
428 		 * for QMAGIC, a_entry is 20 bytes beyond the load address
429 		 * to skip the executable header
430 		 */
431 		vmaddr = trunc_page(a_out->a_entry);
432 
433 		/*
434 		 * Map it all into the process's space as a single
435 		 * copy-on-write "data" segment.
436 		 */
437 		error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
438 		    a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
439 		    MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
440 		if (error)
441 			goto cleanup;
442 	}
443 #ifdef DEBUG
444 	printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
445 	    ((long *)vmaddr)[1]);
446 #endif
447 	if (bss_size != 0) {
448 		/* Calculate BSS start address */
449 		vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
450 		    a_out->a_data;
451 
452 		/* allocate some 'anon' space */
453 		error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
454 		    &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
455 		if (error)
456 			goto cleanup;
457 	}
458 
459 cleanup:
460 	/* Unlock vnode if needed */
461 	if (locked) {
462 		VOP_UNLOCK(vp, 0);
463 		VFS_UNLOCK_GIANT(vfslocked);
464 	}
465 
466 	/* Release the kernel mapping. */
467 	if (a_out)
468 		vm_map_remove(kernel_map, (vm_offset_t)a_out,
469 		    (vm_offset_t)a_out + PAGE_SIZE);
470 
471 	return error;
472 }
473 
474 #endif	/* __i386__ */
475 
476 int
477 linux_select(struct thread *td, struct linux_select_args *args)
478 {
479 	l_timeval ltv;
480 	struct timeval tv0, tv1, utv, *tvp;
481 	int error;
482 
483 #ifdef DEBUG
484 	if (ldebug(select))
485 		printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
486 		    (void *)args->readfds, (void *)args->writefds,
487 		    (void *)args->exceptfds, (void *)args->timeout);
488 #endif
489 
490 	/*
491 	 * Store current time for computation of the amount of
492 	 * time left.
493 	 */
494 	if (args->timeout) {
495 		if ((error = copyin(args->timeout, &ltv, sizeof(ltv))))
496 			goto select_out;
497 		utv.tv_sec = ltv.tv_sec;
498 		utv.tv_usec = ltv.tv_usec;
499 #ifdef DEBUG
500 		if (ldebug(select))
501 			printf(LMSG("incoming timeout (%jd/%ld)"),
502 			    (intmax_t)utv.tv_sec, utv.tv_usec);
503 #endif
504 
505 		if (itimerfix(&utv)) {
506 			/*
507 			 * The timeval was invalid.  Convert it to something
508 			 * valid that will act as it does under Linux.
509 			 */
510 			utv.tv_sec += utv.tv_usec / 1000000;
511 			utv.tv_usec %= 1000000;
512 			if (utv.tv_usec < 0) {
513 				utv.tv_sec -= 1;
514 				utv.tv_usec += 1000000;
515 			}
516 			if (utv.tv_sec < 0)
517 				timevalclear(&utv);
518 		}
519 		microtime(&tv0);
520 		tvp = &utv;
521 	} else
522 		tvp = NULL;
523 
524 	error = kern_select(td, args->nfds, args->readfds, args->writefds,
525 	    args->exceptfds, tvp);
526 
527 #ifdef DEBUG
528 	if (ldebug(select))
529 		printf(LMSG("real select returns %d"), error);
530 #endif
531 	if (error)
532 		goto select_out;
533 
534 	if (args->timeout) {
535 		if (td->td_retval[0]) {
536 			/*
537 			 * Compute how much time was left of the timeout,
538 			 * by subtracting the current time and the time
539 			 * before we started the call, and subtracting
540 			 * that result from the user-supplied value.
541 			 */
542 			microtime(&tv1);
543 			timevalsub(&tv1, &tv0);
544 			timevalsub(&utv, &tv1);
545 			if (utv.tv_sec < 0)
546 				timevalclear(&utv);
547 		} else
548 			timevalclear(&utv);
549 #ifdef DEBUG
550 		if (ldebug(select))
551 			printf(LMSG("outgoing timeout (%jd/%ld)"),
552 			    (intmax_t)utv.tv_sec, utv.tv_usec);
553 #endif
554 		ltv.tv_sec = utv.tv_sec;
555 		ltv.tv_usec = utv.tv_usec;
556 		if ((error = copyout(&ltv, args->timeout, sizeof(ltv))))
557 			goto select_out;
558 	}
559 
560 select_out:
561 #ifdef DEBUG
562 	if (ldebug(select))
563 		printf(LMSG("select_out -> %d"), error);
564 #endif
565 	return error;
566 }
567 
568 int
569 linux_mremap(struct thread *td, struct linux_mremap_args *args)
570 {
571 	struct munmap_args /* {
572 		void *addr;
573 		size_t len;
574 	} */ bsd_args;
575 	int error = 0;
576 
577 #ifdef DEBUG
578 	if (ldebug(mremap))
579 		printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
580 		    (void *)(uintptr_t)args->addr,
581 		    (unsigned long)args->old_len,
582 		    (unsigned long)args->new_len,
583 		    (unsigned long)args->flags);
584 #endif
585 
586 	if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
587 		td->td_retval[0] = 0;
588 		return (EINVAL);
589 	}
590 
591 	/*
592 	 * Check for the page alignment.
593 	 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
594 	 */
595 	if (args->addr & PAGE_MASK) {
596 		td->td_retval[0] = 0;
597 		return (EINVAL);
598 	}
599 
600 	args->new_len = round_page(args->new_len);
601 	args->old_len = round_page(args->old_len);
602 
603 	if (args->new_len > args->old_len) {
604 		td->td_retval[0] = 0;
605 		return ENOMEM;
606 	}
607 
608 	if (args->new_len < args->old_len) {
609 		bsd_args.addr =
610 		    (caddr_t)((uintptr_t)args->addr + args->new_len);
611 		bsd_args.len = args->old_len - args->new_len;
612 		error = munmap(td, &bsd_args);
613 	}
614 
615 	td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
616 	return error;
617 }
618 
619 #define LINUX_MS_ASYNC       0x0001
620 #define LINUX_MS_INVALIDATE  0x0002
621 #define LINUX_MS_SYNC        0x0004
622 
623 int
624 linux_msync(struct thread *td, struct linux_msync_args *args)
625 {
626 	struct msync_args bsd_args;
627 
628 	bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
629 	bsd_args.len = (uintptr_t)args->len;
630 	bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
631 
632 	return msync(td, &bsd_args);
633 }
634 
635 int
636 linux_time(struct thread *td, struct linux_time_args *args)
637 {
638 	struct timeval tv;
639 	l_time_t tm;
640 	int error;
641 
642 #ifdef DEBUG
643 	if (ldebug(time))
644 		printf(ARGS(time, "*"));
645 #endif
646 
647 	microtime(&tv);
648 	tm = tv.tv_sec;
649 	if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
650 		return error;
651 	td->td_retval[0] = tm;
652 	return 0;
653 }
654 
655 struct l_times_argv {
656 	l_long	tms_utime;
657 	l_long	tms_stime;
658 	l_long	tms_cutime;
659 	l_long	tms_cstime;
660 };
661 
662 #define CLK_TCK 100			/* Linux uses 100 */
663 
664 #define CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
665 
666 int
667 linux_times(struct thread *td, struct linux_times_args *args)
668 {
669 	struct timeval tv, utime, stime, cutime, cstime;
670 	struct l_times_argv tms;
671 	struct proc *p;
672 	int error;
673 
674 #ifdef DEBUG
675 	if (ldebug(times))
676 		printf(ARGS(times, "*"));
677 #endif
678 
679 	if (args->buf != NULL) {
680 		p = td->td_proc;
681 		PROC_LOCK(p);
682 		PROC_SLOCK(p);
683 		calcru(p, &utime, &stime);
684 		PROC_SUNLOCK(p);
685 		calccru(p, &cutime, &cstime);
686 		PROC_UNLOCK(p);
687 
688 		tms.tms_utime = CONVTCK(utime);
689 		tms.tms_stime = CONVTCK(stime);
690 
691 		tms.tms_cutime = CONVTCK(cutime);
692 		tms.tms_cstime = CONVTCK(cstime);
693 
694 		if ((error = copyout(&tms, args->buf, sizeof(tms))))
695 			return error;
696 	}
697 
698 	microuptime(&tv);
699 	td->td_retval[0] = (int)CONVTCK(tv);
700 	return 0;
701 }
702 
703 int
704 linux_newuname(struct thread *td, struct linux_newuname_args *args)
705 {
706 	INIT_VPROCG(TD_TO_VPROCG(td));
707 	struct l_new_utsname utsname;
708 	char osname[LINUX_MAX_UTSNAME];
709 	char osrelease[LINUX_MAX_UTSNAME];
710 	char *p;
711 
712 #ifdef DEBUG
713 	if (ldebug(newuname))
714 		printf(ARGS(newuname, "*"));
715 #endif
716 
717 	linux_get_osname(td, osname);
718 	linux_get_osrelease(td, osrelease);
719 
720 	bzero(&utsname, sizeof(utsname));
721 	strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
722 	getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
723 	strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
724 	strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
725 	for (p = utsname.version; *p != '\0'; ++p)
726 		if (*p == '\n') {
727 			*p = '\0';
728 			break;
729 		}
730 	strlcpy(utsname.machine, linux_platform, LINUX_MAX_UTSNAME);
731 
732 	mtx_lock(&hostname_mtx);
733 	strlcpy(utsname.domainname, V_domainname, LINUX_MAX_UTSNAME);
734 	mtx_unlock(&hostname_mtx);
735 
736 	return (copyout(&utsname, args->buf, sizeof(utsname)));
737 }
738 
739 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
740 struct l_utimbuf {
741 	l_time_t l_actime;
742 	l_time_t l_modtime;
743 };
744 
745 int
746 linux_utime(struct thread *td, struct linux_utime_args *args)
747 {
748 	struct timeval tv[2], *tvp;
749 	struct l_utimbuf lut;
750 	char *fname;
751 	int error;
752 
753 	LCONVPATHEXIST(td, args->fname, &fname);
754 
755 #ifdef DEBUG
756 	if (ldebug(utime))
757 		printf(ARGS(utime, "%s, *"), fname);
758 #endif
759 
760 	if (args->times) {
761 		if ((error = copyin(args->times, &lut, sizeof lut))) {
762 			LFREEPATH(fname);
763 			return error;
764 		}
765 		tv[0].tv_sec = lut.l_actime;
766 		tv[0].tv_usec = 0;
767 		tv[1].tv_sec = lut.l_modtime;
768 		tv[1].tv_usec = 0;
769 		tvp = tv;
770 	} else
771 		tvp = NULL;
772 
773 	error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
774 	LFREEPATH(fname);
775 	return (error);
776 }
777 
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_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
806 	LFREEPATH(fname);
807 	return (error);
808 }
809 
810 int
811 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
812 {
813 	l_timeval ltv[2];
814 	struct timeval tv[2], *tvp = NULL;
815 	char *fname;
816 	int error, dfd;
817 
818 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
819 	LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
820 
821 #ifdef DEBUG
822 	if (ldebug(futimesat))
823 		printf(ARGS(futimesat, "%s, *"), fname);
824 #endif
825 
826 	if (args->utimes != NULL) {
827 		if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
828 			LFREEPATH(fname);
829 			return (error);
830 		}
831 		tv[0].tv_sec = ltv[0].tv_sec;
832 		tv[0].tv_usec = ltv[0].tv_usec;
833 		tv[1].tv_sec = ltv[1].tv_sec;
834 		tv[1].tv_usec = ltv[1].tv_usec;
835 		tvp = tv;
836 	}
837 
838 	error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
839 	LFREEPATH(fname);
840 	return (error);
841 }
842 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
843 
844 #define __WCLONE 0x80000000
845 
846 int
847 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
848 {
849 	int error, options, tmpstat;
850 
851 #ifdef DEBUG
852 	if (ldebug(waitpid))
853 		printf(ARGS(waitpid, "%d, %p, %d"),
854 		    args->pid, (void *)args->status, args->options);
855 #endif
856 	/*
857 	 * this is necessary because the test in kern_wait doesn't work
858 	 * because we mess with the options here
859 	 */
860 	if (args->options & ~(WUNTRACED | WNOHANG | WCONTINUED | __WCLONE))
861 		return (EINVAL);
862 
863 	options = (args->options & (WNOHANG | WUNTRACED));
864 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
865 	if (args->options & __WCLONE)
866 		options |= WLINUXCLONE;
867 
868 	error = kern_wait(td, args->pid, &tmpstat, options, NULL);
869 	if (error)
870 		return error;
871 
872 	if (args->status) {
873 		tmpstat &= 0xffff;
874 		if (WIFSIGNALED(tmpstat))
875 			tmpstat = (tmpstat & 0xffffff80) |
876 			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
877 		else if (WIFSTOPPED(tmpstat))
878 			tmpstat = (tmpstat & 0xffff00ff) |
879 			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
880 		return copyout(&tmpstat, args->status, sizeof(int));
881 	}
882 
883 	return 0;
884 }
885 
886 int
887 linux_wait4(struct thread *td, struct linux_wait4_args *args)
888 {
889 	int error, options, tmpstat;
890 	struct rusage ru, *rup;
891 	struct proc *p;
892 
893 #ifdef DEBUG
894 	if (ldebug(wait4))
895 		printf(ARGS(wait4, "%d, %p, %d, %p"),
896 		    args->pid, (void *)args->status, args->options,
897 		    (void *)args->rusage);
898 #endif
899 
900 	options = (args->options & (WNOHANG | WUNTRACED));
901 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
902 	if (args->options & __WCLONE)
903 		options |= WLINUXCLONE;
904 
905 	if (args->rusage != NULL)
906 		rup = &ru;
907 	else
908 		rup = NULL;
909 	error = kern_wait(td, args->pid, &tmpstat, options, rup);
910 	if (error)
911 		return error;
912 
913 	p = td->td_proc;
914 	PROC_LOCK(p);
915 	sigqueue_delete(&p->p_sigqueue, SIGCHLD);
916 	PROC_UNLOCK(p);
917 
918 	if (args->status) {
919 		tmpstat &= 0xffff;
920 		if (WIFSIGNALED(tmpstat))
921 			tmpstat = (tmpstat & 0xffffff80) |
922 			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
923 		else if (WIFSTOPPED(tmpstat))
924 			tmpstat = (tmpstat & 0xffff00ff) |
925 			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
926 		error = copyout(&tmpstat, args->status, sizeof(int));
927 	}
928 	if (args->rusage != NULL && error == 0)
929 		error = copyout(&ru, args->rusage, sizeof(ru));
930 
931 	return (error);
932 }
933 
934 int
935 linux_mknod(struct thread *td, struct linux_mknod_args *args)
936 {
937 	char *path;
938 	int error;
939 
940 	LCONVPATHCREAT(td, args->path, &path);
941 
942 #ifdef DEBUG
943 	if (ldebug(mknod))
944 		printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
945 #endif
946 
947 	switch (args->mode & S_IFMT) {
948 	case S_IFIFO:
949 	case S_IFSOCK:
950 		error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
951 		break;
952 
953 	case S_IFCHR:
954 	case S_IFBLK:
955 		error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
956 		    args->dev);
957 		break;
958 
959 	case S_IFDIR:
960 		error = EPERM;
961 		break;
962 
963 	case 0:
964 		args->mode |= S_IFREG;
965 		/* FALLTHROUGH */
966 	case S_IFREG:
967 		error = kern_open(td, path, UIO_SYSSPACE,
968 		    O_WRONLY | O_CREAT | O_TRUNC, args->mode);
969 		if (error == 0)
970 			kern_close(td, td->td_retval[0]);
971 		break;
972 
973 	default:
974 		error = EINVAL;
975 		break;
976 	}
977 	LFREEPATH(path);
978 	return (error);
979 }
980 
981 int
982 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
983 {
984 	char *path;
985 	int error, dfd;
986 
987 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
988 	LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
989 
990 #ifdef DEBUG
991 	if (ldebug(mknodat))
992 		printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev);
993 #endif
994 
995 	switch (args->mode & S_IFMT) {
996 	case S_IFIFO:
997 	case S_IFSOCK:
998 		error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
999 		break;
1000 
1001 	case S_IFCHR:
1002 	case S_IFBLK:
1003 		error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
1004 		    args->dev);
1005 		break;
1006 
1007 	case S_IFDIR:
1008 		error = EPERM;
1009 		break;
1010 
1011 	case 0:
1012 		args->mode |= S_IFREG;
1013 		/* FALLTHROUGH */
1014 	case S_IFREG:
1015 		error = kern_openat(td, dfd, path, UIO_SYSSPACE,
1016 		    O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1017 		if (error == 0)
1018 			kern_close(td, td->td_retval[0]);
1019 		break;
1020 
1021 	default:
1022 		error = EINVAL;
1023 		break;
1024 	}
1025 	LFREEPATH(path);
1026 	return (error);
1027 }
1028 
1029 /*
1030  * UGH! This is just about the dumbest idea I've ever heard!!
1031  */
1032 int
1033 linux_personality(struct thread *td, struct linux_personality_args *args)
1034 {
1035 #ifdef DEBUG
1036 	if (ldebug(personality))
1037 		printf(ARGS(personality, "%lu"), (unsigned long)args->per);
1038 #endif
1039 	if (args->per != 0)
1040 		return EINVAL;
1041 
1042 	/* Yes Jim, it's still a Linux... */
1043 	td->td_retval[0] = 0;
1044 	return 0;
1045 }
1046 
1047 struct l_itimerval {
1048 	l_timeval it_interval;
1049 	l_timeval it_value;
1050 };
1051 
1052 #define	B2L_ITIMERVAL(bip, lip) 					\
1053 	(bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec;		\
1054 	(bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec;	\
1055 	(bip)->it_value.tv_sec = (lip)->it_value.tv_sec;		\
1056 	(bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1057 
1058 int
1059 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1060 {
1061 	int error;
1062 	struct l_itimerval ls;
1063 	struct itimerval aitv, oitv;
1064 
1065 #ifdef DEBUG
1066 	if (ldebug(setitimer))
1067 		printf(ARGS(setitimer, "%p, %p"),
1068 		    (void *)uap->itv, (void *)uap->oitv);
1069 #endif
1070 
1071 	if (uap->itv == NULL) {
1072 		uap->itv = uap->oitv;
1073 		return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1074 	}
1075 
1076 	error = copyin(uap->itv, &ls, sizeof(ls));
1077 	if (error != 0)
1078 		return (error);
1079 	B2L_ITIMERVAL(&aitv, &ls);
1080 #ifdef DEBUG
1081 	if (ldebug(setitimer)) {
1082 		printf("setitimer: value: sec: %jd, usec: %ld\n",
1083 		    (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1084 		printf("setitimer: interval: sec: %jd, usec: %ld\n",
1085 		    (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1086 	}
1087 #endif
1088 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
1089 	if (error != 0 || uap->oitv == NULL)
1090 		return (error);
1091 	B2L_ITIMERVAL(&ls, &oitv);
1092 
1093 	return (copyout(&ls, uap->oitv, sizeof(ls)));
1094 }
1095 
1096 int
1097 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1098 {
1099 	int error;
1100 	struct l_itimerval ls;
1101 	struct itimerval aitv;
1102 
1103 #ifdef DEBUG
1104 	if (ldebug(getitimer))
1105 		printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1106 #endif
1107 	error = kern_getitimer(td, uap->which, &aitv);
1108 	if (error != 0)
1109 		return (error);
1110 	B2L_ITIMERVAL(&ls, &aitv);
1111 	return (copyout(&ls, uap->itv, sizeof(ls)));
1112 }
1113 
1114 int
1115 linux_nice(struct thread *td, struct linux_nice_args *args)
1116 {
1117 	struct setpriority_args bsd_args;
1118 
1119 	bsd_args.which = PRIO_PROCESS;
1120 	bsd_args.who = 0;		/* current process */
1121 	bsd_args.prio = args->inc;
1122 	return setpriority(td, &bsd_args);
1123 }
1124 
1125 int
1126 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1127 {
1128 	struct ucred *newcred, *oldcred;
1129 	l_gid_t linux_gidset[NGROUPS];
1130 	gid_t *bsd_gidset;
1131 	int ngrp, error;
1132 	struct proc *p;
1133 
1134 	ngrp = args->gidsetsize;
1135 	if (ngrp < 0 || ngrp >= NGROUPS)
1136 		return (EINVAL);
1137 	error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1138 	if (error)
1139 		return (error);
1140 	newcred = crget();
1141 	p = td->td_proc;
1142 	PROC_LOCK(p);
1143 	oldcred = p->p_ucred;
1144 
1145 	/*
1146 	 * cr_groups[0] holds egid. Setting the whole set from
1147 	 * the supplied set will cause egid to be changed too.
1148 	 * Keep cr_groups[0] unchanged to prevent that.
1149 	 */
1150 
1151 	if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
1152 		PROC_UNLOCK(p);
1153 		crfree(newcred);
1154 		return (error);
1155 	}
1156 
1157 	crcopy(newcred, oldcred);
1158 	if (ngrp > 0) {
1159 		newcred->cr_ngroups = ngrp + 1;
1160 
1161 		bsd_gidset = newcred->cr_groups;
1162 		ngrp--;
1163 		while (ngrp >= 0) {
1164 			bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1165 			ngrp--;
1166 		}
1167 	} else
1168 		newcred->cr_ngroups = 1;
1169 
1170 	setsugid(p);
1171 	p->p_ucred = newcred;
1172 	PROC_UNLOCK(p);
1173 	crfree(oldcred);
1174 	return (0);
1175 }
1176 
1177 int
1178 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1179 {
1180 	struct ucred *cred;
1181 	l_gid_t linux_gidset[NGROUPS];
1182 	gid_t *bsd_gidset;
1183 	int bsd_gidsetsz, ngrp, error;
1184 
1185 	cred = td->td_ucred;
1186 	bsd_gidset = cred->cr_groups;
1187 	bsd_gidsetsz = cred->cr_ngroups - 1;
1188 
1189 	/*
1190 	 * cr_groups[0] holds egid. Returning the whole set
1191 	 * here will cause a duplicate. Exclude cr_groups[0]
1192 	 * to prevent that.
1193 	 */
1194 
1195 	if ((ngrp = args->gidsetsize) == 0) {
1196 		td->td_retval[0] = bsd_gidsetsz;
1197 		return (0);
1198 	}
1199 
1200 	if (ngrp < bsd_gidsetsz)
1201 		return (EINVAL);
1202 
1203 	ngrp = 0;
1204 	while (ngrp < bsd_gidsetsz) {
1205 		linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1206 		ngrp++;
1207 	}
1208 
1209 	if ((error = copyout(linux_gidset, args->grouplist,
1210 	    ngrp * sizeof(l_gid_t))))
1211 		return (error);
1212 
1213 	td->td_retval[0] = ngrp;
1214 	return (0);
1215 }
1216 
1217 int
1218 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1219 {
1220 	struct rlimit bsd_rlim;
1221 	struct l_rlimit rlim;
1222 	u_int which;
1223 	int error;
1224 
1225 #ifdef DEBUG
1226 	if (ldebug(setrlimit))
1227 		printf(ARGS(setrlimit, "%d, %p"),
1228 		    args->resource, (void *)args->rlim);
1229 #endif
1230 
1231 	if (args->resource >= LINUX_RLIM_NLIMITS)
1232 		return (EINVAL);
1233 
1234 	which = linux_to_bsd_resource[args->resource];
1235 	if (which == -1)
1236 		return (EINVAL);
1237 
1238 	error = copyin(args->rlim, &rlim, sizeof(rlim));
1239 	if (error)
1240 		return (error);
1241 
1242 	bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1243 	bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1244 	return (kern_setrlimit(td, which, &bsd_rlim));
1245 }
1246 
1247 int
1248 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1249 {
1250 	struct l_rlimit rlim;
1251 	struct proc *p = td->td_proc;
1252 	struct rlimit bsd_rlim;
1253 	u_int which;
1254 
1255 #ifdef DEBUG
1256 	if (ldebug(old_getrlimit))
1257 		printf(ARGS(old_getrlimit, "%d, %p"),
1258 		    args->resource, (void *)args->rlim);
1259 #endif
1260 
1261 	if (args->resource >= LINUX_RLIM_NLIMITS)
1262 		return (EINVAL);
1263 
1264 	which = linux_to_bsd_resource[args->resource];
1265 	if (which == -1)
1266 		return (EINVAL);
1267 
1268 	PROC_LOCK(p);
1269 	lim_rlimit(p, which, &bsd_rlim);
1270 	PROC_UNLOCK(p);
1271 
1272 #ifdef COMPAT_LINUX32
1273 	rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1274 	if (rlim.rlim_cur == UINT_MAX)
1275 		rlim.rlim_cur = INT_MAX;
1276 	rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1277 	if (rlim.rlim_max == UINT_MAX)
1278 		rlim.rlim_max = INT_MAX;
1279 #else
1280 	rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1281 	if (rlim.rlim_cur == ULONG_MAX)
1282 		rlim.rlim_cur = LONG_MAX;
1283 	rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1284 	if (rlim.rlim_max == ULONG_MAX)
1285 		rlim.rlim_max = LONG_MAX;
1286 #endif
1287 	return (copyout(&rlim, args->rlim, sizeof(rlim)));
1288 }
1289 
1290 int
1291 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1292 {
1293 	struct l_rlimit rlim;
1294 	struct proc *p = td->td_proc;
1295 	struct rlimit bsd_rlim;
1296 	u_int which;
1297 
1298 #ifdef DEBUG
1299 	if (ldebug(getrlimit))
1300 		printf(ARGS(getrlimit, "%d, %p"),
1301 		    args->resource, (void *)args->rlim);
1302 #endif
1303 
1304 	if (args->resource >= LINUX_RLIM_NLIMITS)
1305 		return (EINVAL);
1306 
1307 	which = linux_to_bsd_resource[args->resource];
1308 	if (which == -1)
1309 		return (EINVAL);
1310 
1311 	PROC_LOCK(p);
1312 	lim_rlimit(p, which, &bsd_rlim);
1313 	PROC_UNLOCK(p);
1314 
1315 	rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1316 	rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1317 	return (copyout(&rlim, args->rlim, sizeof(rlim)));
1318 }
1319 
1320 int
1321 linux_sched_setscheduler(struct thread *td,
1322     struct linux_sched_setscheduler_args *args)
1323 {
1324 	struct sched_setscheduler_args bsd;
1325 
1326 #ifdef DEBUG
1327 	if (ldebug(sched_setscheduler))
1328 		printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1329 		    args->pid, args->policy, (const void *)args->param);
1330 #endif
1331 
1332 	switch (args->policy) {
1333 	case LINUX_SCHED_OTHER:
1334 		bsd.policy = SCHED_OTHER;
1335 		break;
1336 	case LINUX_SCHED_FIFO:
1337 		bsd.policy = SCHED_FIFO;
1338 		break;
1339 	case LINUX_SCHED_RR:
1340 		bsd.policy = SCHED_RR;
1341 		break;
1342 	default:
1343 		return EINVAL;
1344 	}
1345 
1346 	bsd.pid = args->pid;
1347 	bsd.param = (struct sched_param *)args->param;
1348 	return sched_setscheduler(td, &bsd);
1349 }
1350 
1351 int
1352 linux_sched_getscheduler(struct thread *td,
1353     struct linux_sched_getscheduler_args *args)
1354 {
1355 	struct sched_getscheduler_args bsd;
1356 	int error;
1357 
1358 #ifdef DEBUG
1359 	if (ldebug(sched_getscheduler))
1360 		printf(ARGS(sched_getscheduler, "%d"), args->pid);
1361 #endif
1362 
1363 	bsd.pid = args->pid;
1364 	error = sched_getscheduler(td, &bsd);
1365 
1366 	switch (td->td_retval[0]) {
1367 	case SCHED_OTHER:
1368 		td->td_retval[0] = LINUX_SCHED_OTHER;
1369 		break;
1370 	case SCHED_FIFO:
1371 		td->td_retval[0] = LINUX_SCHED_FIFO;
1372 		break;
1373 	case SCHED_RR:
1374 		td->td_retval[0] = LINUX_SCHED_RR;
1375 		break;
1376 	}
1377 
1378 	return error;
1379 }
1380 
1381 int
1382 linux_sched_get_priority_max(struct thread *td,
1383     struct linux_sched_get_priority_max_args *args)
1384 {
1385 	struct sched_get_priority_max_args bsd;
1386 
1387 #ifdef DEBUG
1388 	if (ldebug(sched_get_priority_max))
1389 		printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1390 #endif
1391 
1392 	switch (args->policy) {
1393 	case LINUX_SCHED_OTHER:
1394 		bsd.policy = SCHED_OTHER;
1395 		break;
1396 	case LINUX_SCHED_FIFO:
1397 		bsd.policy = SCHED_FIFO;
1398 		break;
1399 	case LINUX_SCHED_RR:
1400 		bsd.policy = SCHED_RR;
1401 		break;
1402 	default:
1403 		return EINVAL;
1404 	}
1405 	return sched_get_priority_max(td, &bsd);
1406 }
1407 
1408 int
1409 linux_sched_get_priority_min(struct thread *td,
1410     struct linux_sched_get_priority_min_args *args)
1411 {
1412 	struct sched_get_priority_min_args bsd;
1413 
1414 #ifdef DEBUG
1415 	if (ldebug(sched_get_priority_min))
1416 		printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1417 #endif
1418 
1419 	switch (args->policy) {
1420 	case LINUX_SCHED_OTHER:
1421 		bsd.policy = SCHED_OTHER;
1422 		break;
1423 	case LINUX_SCHED_FIFO:
1424 		bsd.policy = SCHED_FIFO;
1425 		break;
1426 	case LINUX_SCHED_RR:
1427 		bsd.policy = SCHED_RR;
1428 		break;
1429 	default:
1430 		return EINVAL;
1431 	}
1432 	return sched_get_priority_min(td, &bsd);
1433 }
1434 
1435 #define REBOOT_CAD_ON	0x89abcdef
1436 #define REBOOT_CAD_OFF	0
1437 #define REBOOT_HALT	0xcdef0123
1438 #define REBOOT_RESTART	0x01234567
1439 #define REBOOT_RESTART2	0xA1B2C3D4
1440 #define REBOOT_POWEROFF	0x4321FEDC
1441 #define REBOOT_MAGIC1	0xfee1dead
1442 #define REBOOT_MAGIC2	0x28121969
1443 #define REBOOT_MAGIC2A	0x05121996
1444 #define REBOOT_MAGIC2B	0x16041998
1445 
1446 int
1447 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1448 {
1449 	struct reboot_args bsd_args;
1450 
1451 #ifdef DEBUG
1452 	if (ldebug(reboot))
1453 		printf(ARGS(reboot, "0x%x"), args->cmd);
1454 #endif
1455 
1456 	if (args->magic1 != REBOOT_MAGIC1)
1457 		return EINVAL;
1458 
1459 	switch (args->magic2) {
1460 	case REBOOT_MAGIC2:
1461 	case REBOOT_MAGIC2A:
1462 	case REBOOT_MAGIC2B:
1463 		break;
1464 	default:
1465 		return EINVAL;
1466 	}
1467 
1468 	switch (args->cmd) {
1469 	case REBOOT_CAD_ON:
1470 	case REBOOT_CAD_OFF:
1471 		return (priv_check(td, PRIV_REBOOT));
1472 	case REBOOT_HALT:
1473 		bsd_args.opt = RB_HALT;
1474 		break;
1475 	case REBOOT_RESTART:
1476 	case REBOOT_RESTART2:
1477 		bsd_args.opt = 0;
1478 		break;
1479 	case REBOOT_POWEROFF:
1480 		bsd_args.opt = RB_POWEROFF;
1481 		break;
1482 	default:
1483 		return EINVAL;
1484 	}
1485 	return reboot(td, &bsd_args);
1486 }
1487 
1488 
1489 /*
1490  * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1491  * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1492  * are assumed to be preserved. The following lightweight syscalls fixes
1493  * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1494  *
1495  * linux_getpid() - MP SAFE
1496  * linux_getgid() - MP SAFE
1497  * linux_getuid() - MP SAFE
1498  */
1499 
1500 int
1501 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1502 {
1503 	struct linux_emuldata *em;
1504 
1505 #ifdef DEBUG
1506 	if (ldebug(getpid))
1507 		printf(ARGS(getpid, ""));
1508 #endif
1509 
1510 	if (linux_use26(td)) {
1511 		em = em_find(td->td_proc, EMUL_DONTLOCK);
1512 		KASSERT(em != NULL, ("getpid: emuldata not found.\n"));
1513 		td->td_retval[0] = em->shared->group_pid;
1514 	} else {
1515 		td->td_retval[0] = td->td_proc->p_pid;
1516 	}
1517 
1518 	return (0);
1519 }
1520 
1521 int
1522 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1523 {
1524 
1525 #ifdef DEBUG
1526 	if (ldebug(gettid))
1527 		printf(ARGS(gettid, ""));
1528 #endif
1529 
1530 	td->td_retval[0] = td->td_proc->p_pid;
1531 	return (0);
1532 }
1533 
1534 
1535 int
1536 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1537 {
1538 	struct linux_emuldata *em;
1539 	struct proc *p, *pp;
1540 
1541 #ifdef DEBUG
1542 	if (ldebug(getppid))
1543 		printf(ARGS(getppid, ""));
1544 #endif
1545 
1546 	if (!linux_use26(td)) {
1547 		PROC_LOCK(td->td_proc);
1548 		td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1549 		PROC_UNLOCK(td->td_proc);
1550 		return (0);
1551 	}
1552 
1553 	em = em_find(td->td_proc, EMUL_DONTLOCK);
1554 
1555 	KASSERT(em != NULL, ("getppid: process emuldata not found.\n"));
1556 
1557 	/* find the group leader */
1558 	p = pfind(em->shared->group_pid);
1559 
1560 	if (p == NULL) {
1561 #ifdef DEBUG
1562 	   	printf(LMSG("parent process not found.\n"));
1563 #endif
1564 		return (0);
1565 	}
1566 
1567 	pp = p->p_pptr;		/* switch to parent */
1568 	PROC_LOCK(pp);
1569 	PROC_UNLOCK(p);
1570 
1571 	/* if its also linux process */
1572 	if (pp->p_sysent == &elf_linux_sysvec) {
1573 		em = em_find(pp, EMUL_DONTLOCK);
1574 		KASSERT(em != NULL, ("getppid: parent emuldata not found.\n"));
1575 
1576 		td->td_retval[0] = em->shared->group_pid;
1577 	} else
1578 		td->td_retval[0] = pp->p_pid;
1579 
1580 	PROC_UNLOCK(pp);
1581 
1582 	return (0);
1583 }
1584 
1585 int
1586 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1587 {
1588 
1589 #ifdef DEBUG
1590 	if (ldebug(getgid))
1591 		printf(ARGS(getgid, ""));
1592 #endif
1593 
1594 	td->td_retval[0] = td->td_ucred->cr_rgid;
1595 	return (0);
1596 }
1597 
1598 int
1599 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1600 {
1601 
1602 #ifdef DEBUG
1603 	if (ldebug(getuid))
1604 		printf(ARGS(getuid, ""));
1605 #endif
1606 
1607 	td->td_retval[0] = td->td_ucred->cr_ruid;
1608 	return (0);
1609 }
1610 
1611 
1612 int
1613 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1614 {
1615 	struct getsid_args bsd;
1616 
1617 #ifdef DEBUG
1618 	if (ldebug(getsid))
1619 		printf(ARGS(getsid, "%i"), args->pid);
1620 #endif
1621 
1622 	bsd.pid = args->pid;
1623 	return getsid(td, &bsd);
1624 }
1625 
1626 int
1627 linux_nosys(struct thread *td, struct nosys_args *ignore)
1628 {
1629 
1630 	return (ENOSYS);
1631 }
1632 
1633 int
1634 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1635 {
1636 	struct getpriority_args bsd_args;
1637 	int error;
1638 
1639 #ifdef DEBUG
1640 	if (ldebug(getpriority))
1641 		printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1642 #endif
1643 
1644 	bsd_args.which = args->which;
1645 	bsd_args.who = args->who;
1646 	error = getpriority(td, &bsd_args);
1647 	td->td_retval[0] = 20 - td->td_retval[0];
1648 	return error;
1649 }
1650 
1651 int
1652 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1653 {
1654 	int name[2];
1655 
1656 #ifdef DEBUG
1657 	if (ldebug(sethostname))
1658 		printf(ARGS(sethostname, "*, %i"), args->len);
1659 #endif
1660 
1661 	name[0] = CTL_KERN;
1662 	name[1] = KERN_HOSTNAME;
1663 	return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1664 	    args->len, 0, 0));
1665 }
1666 
1667 int
1668 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1669 {
1670 	int name[2];
1671 
1672 #ifdef DEBUG
1673 	if (ldebug(setdomainname))
1674 		printf(ARGS(setdomainname, "*, %i"), args->len);
1675 #endif
1676 
1677 	name[0] = CTL_KERN;
1678 	name[1] = KERN_NISDOMAINNAME;
1679 	return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1680 	    args->len, 0, 0));
1681 }
1682 
1683 int
1684 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1685 {
1686 	struct linux_emuldata *em, *td_em, *tmp_em;
1687 	struct proc *sp;
1688 
1689 #ifdef DEBUG
1690 	if (ldebug(exit_group))
1691 		printf(ARGS(exit_group, "%i"), args->error_code);
1692 #endif
1693 
1694 	if (linux_use26(td)) {
1695 		td_em = em_find(td->td_proc, EMUL_DONTLOCK);
1696 
1697 		KASSERT(td_em != NULL, ("exit_group: emuldata not found.\n"));
1698 
1699 		EMUL_SHARED_RLOCK(&emul_shared_lock);
1700 		LIST_FOREACH_SAFE(em, &td_em->shared->threads, threads, tmp_em) {
1701 			if (em->pid == td_em->pid)
1702 				continue;
1703 
1704 			sp = pfind(em->pid);
1705 			psignal(sp, SIGKILL);
1706 			PROC_UNLOCK(sp);
1707 #ifdef DEBUG
1708 			printf(LMSG("linux_sys_exit_group: kill PID %d\n"), em->pid);
1709 #endif
1710 		}
1711 
1712 		EMUL_SHARED_RUNLOCK(&emul_shared_lock);
1713 	}
1714 	/*
1715 	 * XXX: we should send a signal to the parent if
1716 	 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1717 	 * as it doesnt occur often.
1718 	 */
1719 	exit1(td, W_EXITCODE(args->error_code, 0));
1720 
1721 	return (0);
1722 }
1723 
1724 int
1725 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1726 {
1727 	int error = 0, max_size;
1728 	struct proc *p = td->td_proc;
1729 	char comm[LINUX_MAX_COMM_LEN];
1730 	struct linux_emuldata *em;
1731 	int pdeath_signal;
1732 
1733 #ifdef DEBUG
1734 	if (ldebug(prctl))
1735 		printf(ARGS(prctl, "%d, %d, %d, %d, %d"), args->option,
1736 		    args->arg2, args->arg3, args->arg4, args->arg5);
1737 #endif
1738 
1739 	switch (args->option) {
1740 	case LINUX_PR_SET_PDEATHSIG:
1741 		if (!LINUX_SIG_VALID(args->arg2))
1742 			return (EINVAL);
1743 		em = em_find(p, EMUL_DOLOCK);
1744 		KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1745 		em->pdeath_signal = args->arg2;
1746 		EMUL_UNLOCK(&emul_lock);
1747 		break;
1748 	case LINUX_PR_GET_PDEATHSIG:
1749 		em = em_find(p, EMUL_DOLOCK);
1750 		KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1751 		pdeath_signal = em->pdeath_signal;
1752 		EMUL_UNLOCK(&emul_lock);
1753 		error = copyout(&pdeath_signal,
1754 		    (void *)(register_t)args->arg2,
1755 		    sizeof(pdeath_signal));
1756 		break;
1757 	case LINUX_PR_SET_NAME:
1758 		/*
1759 		 * To be on the safe side we need to make sure to not
1760 		 * overflow the size a linux program expects. We already
1761 		 * do this here in the copyin, so that we don't need to
1762 		 * check on copyout.
1763 		 */
1764 		max_size = MIN(sizeof(comm), sizeof(p->p_comm));
1765 		error = copyinstr((void *)(register_t)args->arg2, comm,
1766 		    max_size, NULL);
1767 
1768 		/* Linux silently truncates the name if it is too long. */
1769 		if (error == ENAMETOOLONG) {
1770 			/*
1771 			 * XXX: copyinstr() isn't documented to populate the
1772 			 * array completely, so do a copyin() to be on the
1773 			 * safe side. This should be changed in case
1774 			 * copyinstr() is changed to guarantee this.
1775 			 */
1776 			error = copyin((void *)(register_t)args->arg2, comm,
1777 			    max_size - 1);
1778 			comm[max_size - 1] = '\0';
1779 		}
1780 		if (error)
1781 			return (error);
1782 
1783 		PROC_LOCK(p);
1784 		strlcpy(p->p_comm, comm, sizeof(p->p_comm));
1785 		PROC_UNLOCK(p);
1786 		break;
1787 	case LINUX_PR_GET_NAME:
1788 		PROC_LOCK(p);
1789 		strlcpy(comm, p->p_comm, sizeof(comm));
1790 		PROC_UNLOCK(p);
1791 		error = copyout(comm, (void *)(register_t)args->arg2,
1792 		    strlen(comm) + 1);
1793 		break;
1794 	default:
1795 		error = EINVAL;
1796 		break;
1797 	}
1798 
1799 	return (error);
1800 }
1801 
1802 /*
1803  * Get affinity of a process.
1804  */
1805 int
1806 linux_sched_getaffinity(struct thread *td,
1807     struct linux_sched_getaffinity_args *args)
1808 {
1809 	int error;
1810 	struct cpuset_getaffinity_args cga;
1811 
1812 #ifdef DEBUG
1813 	if (ldebug(sched_getaffinity))
1814 		printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
1815 		    args->len);
1816 #endif
1817 	if (args->len < sizeof(cpuset_t))
1818 		return (EINVAL);
1819 
1820 	cga.level = CPU_LEVEL_WHICH;
1821 	cga.which = CPU_WHICH_PID;
1822 	cga.id = args->pid;
1823 	cga.cpusetsize = sizeof(cpuset_t);
1824 	cga.mask = (cpuset_t *) args->user_mask_ptr;
1825 
1826 	if ((error = cpuset_getaffinity(td, &cga)) == 0)
1827 		td->td_retval[0] = sizeof(cpuset_t);
1828 
1829 	return (error);
1830 }
1831 
1832 /*
1833  *  Set affinity of a process.
1834  */
1835 int
1836 linux_sched_setaffinity(struct thread *td,
1837     struct linux_sched_setaffinity_args *args)
1838 {
1839 	struct cpuset_setaffinity_args csa;
1840 
1841 #ifdef DEBUG
1842 	if (ldebug(sched_setaffinity))
1843 		printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
1844 		    args->len);
1845 #endif
1846 	if (args->len < sizeof(cpuset_t))
1847 		return (EINVAL);
1848 
1849 	csa.level = CPU_LEVEL_WHICH;
1850 	csa.which = CPU_WHICH_PID;
1851 	csa.id = args->pid;
1852 	csa.cpusetsize = sizeof(cpuset_t);
1853 	csa.mask = (cpuset_t *) args->user_mask_ptr;
1854 
1855 	return (cpuset_setaffinity(td, &csa));
1856 }
1857