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