xref: /freebsd/sys/compat/linux/linux_misc.c (revision 6b0d16d374627cfcb0c39328097ffa6b4fa3d0df)
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 
68 #include <security/mac/mac_framework.h>
69 
70 #include <vm/vm.h>
71 #include <vm/pmap.h>
72 #include <vm/vm_kern.h>
73 #include <vm/vm_map.h>
74 #include <vm/vm_extern.h>
75 #include <vm/vm_object.h>
76 #include <vm/swap_pager.h>
77 
78 #include <compat/linux/linux_sysproto.h>
79 #include <compat/linux/linux_emul.h>
80 #include <compat/linux/linux_misc.h>
81 
82 #ifdef COMPAT_LINUX32
83 #include <machine/../linux32/linux.h>
84 #include <machine/../linux32/linux32_proto.h>
85 #else
86 #include <machine/../linux/linux.h>
87 #include <machine/../linux/linux_proto.h>
88 #endif
89 
90 #include <compat/linux/linux_mib.h>
91 #include <compat/linux/linux_signal.h>
92 #include <compat/linux/linux_util.h>
93 
94 #ifdef __i386__
95 #include <machine/cputypes.h>
96 #endif
97 
98 #define BSD_TO_LINUX_SIGNAL(sig)	\
99 	(((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
100 
101 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
102 	RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
103 	RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
104 	RLIMIT_MEMLOCK, RLIMIT_AS
105 };
106 
107 struct l_sysinfo {
108 	l_long		uptime;		/* Seconds since boot */
109 	l_ulong		loads[3];	/* 1, 5, and 15 minute load averages */
110 #define LINUX_SYSINFO_LOADS_SCALE 65536
111 	l_ulong		totalram;	/* Total usable main memory size */
112 	l_ulong		freeram;	/* Available memory size */
113 	l_ulong		sharedram;	/* Amount of shared memory */
114 	l_ulong		bufferram;	/* Memory used by buffers */
115 	l_ulong		totalswap;	/* Total swap space size */
116 	l_ulong		freeswap;	/* swap space still available */
117 	l_ushort	procs;		/* Number of current processes */
118 	l_ushort	pads;
119 	l_ulong		totalbig;
120 	l_ulong		freebig;
121 	l_uint		mem_unit;
122 	char		_f[20-2*sizeof(l_long)-sizeof(l_int)];	/* padding */
123 };
124 int
125 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
126 {
127 	struct l_sysinfo sysinfo;
128 	vm_object_t object;
129 	int i, j;
130 	struct timespec ts;
131 
132 	getnanouptime(&ts);
133 	if (ts.tv_nsec != 0)
134 		ts.tv_sec++;
135 	sysinfo.uptime = ts.tv_sec;
136 
137 	/* Use the information from the mib to get our load averages */
138 	for (i = 0; i < 3; i++)
139 		sysinfo.loads[i] = averunnable.ldavg[i] *
140 		    LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
141 
142 	sysinfo.totalram = physmem * PAGE_SIZE;
143 	sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
144 
145 	sysinfo.sharedram = 0;
146 	mtx_lock(&vm_object_list_mtx);
147 	TAILQ_FOREACH(object, &vm_object_list, object_list)
148 		if (object->shadow_count > 1)
149 			sysinfo.sharedram += object->resident_page_count;
150 	mtx_unlock(&vm_object_list_mtx);
151 
152 	sysinfo.sharedram *= PAGE_SIZE;
153 	sysinfo.bufferram = 0;
154 
155 	swap_pager_status(&i, &j);
156 	sysinfo.totalswap = i * PAGE_SIZE;
157 	sysinfo.freeswap = (i - j) * PAGE_SIZE;
158 
159 	sysinfo.procs = nprocs;
160 
161 	/* The following are only present in newer Linux kernels. */
162 	sysinfo.totalbig = 0;
163 	sysinfo.freebig = 0;
164 	sysinfo.mem_unit = 1;
165 
166 	return copyout(&sysinfo, args->info, sizeof(sysinfo));
167 }
168 
169 int
170 linux_alarm(struct thread *td, struct linux_alarm_args *args)
171 {
172 	struct itimerval it, old_it;
173 	int error;
174 
175 #ifdef DEBUG
176 	if (ldebug(alarm))
177 		printf(ARGS(alarm, "%u"), args->secs);
178 #endif
179 
180 	if (args->secs > 100000000)
181 		return (EINVAL);
182 
183 	it.it_value.tv_sec = (long)args->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, td);
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, FREAD);
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 		/*
533 		 * See fs/select.c in the Linux kernel.  Without this,
534 		 * Maelstrom doesn't work.
535 		 */
536 		if (error == ERESTART)
537 			error = EINTR;
538 		goto select_out;
539 	}
540 
541 	if (args->timeout) {
542 		if (td->td_retval[0]) {
543 			/*
544 			 * Compute how much time was left of the timeout,
545 			 * by subtracting the current time and the time
546 			 * before we started the call, and subtracting
547 			 * that result from the user-supplied value.
548 			 */
549 			microtime(&tv1);
550 			timevalsub(&tv1, &tv0);
551 			timevalsub(&utv, &tv1);
552 			if (utv.tv_sec < 0)
553 				timevalclear(&utv);
554 		} else
555 			timevalclear(&utv);
556 #ifdef DEBUG
557 		if (ldebug(select))
558 			printf(LMSG("outgoing timeout (%jd/%ld)"),
559 			    (intmax_t)utv.tv_sec, utv.tv_usec);
560 #endif
561 		ltv.tv_sec = utv.tv_sec;
562 		ltv.tv_usec = utv.tv_usec;
563 		if ((error = copyout(&ltv, args->timeout, sizeof(ltv))))
564 			goto select_out;
565 	}
566 
567 select_out:
568 #ifdef DEBUG
569 	if (ldebug(select))
570 		printf(LMSG("select_out -> %d"), error);
571 #endif
572 	return error;
573 }
574 
575 int
576 linux_mremap(struct thread *td, struct linux_mremap_args *args)
577 {
578 	struct munmap_args /* {
579 		void *addr;
580 		size_t len;
581 	} */ bsd_args;
582 	int error = 0;
583 
584 #ifdef DEBUG
585 	if (ldebug(mremap))
586 		printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
587 		    (void *)(uintptr_t)args->addr,
588 		    (unsigned long)args->old_len,
589 		    (unsigned long)args->new_len,
590 		    (unsigned long)args->flags);
591 #endif
592 
593 	if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
594 		td->td_retval[0] = 0;
595 		return (EINVAL);
596 	}
597 
598 	/*
599 	 * Check for the page alignment.
600 	 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
601 	 */
602 	if (args->addr & PAGE_MASK) {
603 		td->td_retval[0] = 0;
604 		return (EINVAL);
605 	}
606 
607 	args->new_len = round_page(args->new_len);
608 	args->old_len = round_page(args->old_len);
609 
610 	if (args->new_len > args->old_len) {
611 		td->td_retval[0] = 0;
612 		return ENOMEM;
613 	}
614 
615 	if (args->new_len < args->old_len) {
616 		bsd_args.addr =
617 		    (caddr_t)((uintptr_t)args->addr + args->new_len);
618 		bsd_args.len = args->old_len - args->new_len;
619 		error = munmap(td, &bsd_args);
620 	}
621 
622 	td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
623 	return error;
624 }
625 
626 #define LINUX_MS_ASYNC       0x0001
627 #define LINUX_MS_INVALIDATE  0x0002
628 #define LINUX_MS_SYNC        0x0004
629 
630 int
631 linux_msync(struct thread *td, struct linux_msync_args *args)
632 {
633 	struct msync_args bsd_args;
634 
635 	bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
636 	bsd_args.len = (uintptr_t)args->len;
637 	bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
638 
639 	return msync(td, &bsd_args);
640 }
641 
642 int
643 linux_time(struct thread *td, struct linux_time_args *args)
644 {
645 	struct timeval tv;
646 	l_time_t tm;
647 	int error;
648 
649 #ifdef DEBUG
650 	if (ldebug(time))
651 		printf(ARGS(time, "*"));
652 #endif
653 
654 	microtime(&tv);
655 	tm = tv.tv_sec;
656 	if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
657 		return error;
658 	td->td_retval[0] = tm;
659 	return 0;
660 }
661 
662 struct l_times_argv {
663 	l_long	tms_utime;
664 	l_long	tms_stime;
665 	l_long	tms_cutime;
666 	l_long	tms_cstime;
667 };
668 
669 #define CLK_TCK 100			/* Linux uses 100 */
670 
671 #define CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
672 
673 int
674 linux_times(struct thread *td, struct linux_times_args *args)
675 {
676 	struct timeval tv, utime, stime, cutime, cstime;
677 	struct l_times_argv tms;
678 	struct proc *p;
679 	int error;
680 
681 #ifdef DEBUG
682 	if (ldebug(times))
683 		printf(ARGS(times, "*"));
684 #endif
685 
686 	if (args->buf != NULL) {
687 		p = td->td_proc;
688 		PROC_LOCK(p);
689 		PROC_SLOCK(p);
690 		calcru(p, &utime, &stime);
691 		PROC_SUNLOCK(p);
692 		calccru(p, &cutime, &cstime);
693 		PROC_UNLOCK(p);
694 
695 		tms.tms_utime = CONVTCK(utime);
696 		tms.tms_stime = CONVTCK(stime);
697 
698 		tms.tms_cutime = CONVTCK(cutime);
699 		tms.tms_cstime = CONVTCK(cstime);
700 
701 		if ((error = copyout(&tms, args->buf, sizeof(tms))))
702 			return error;
703 	}
704 
705 	microuptime(&tv);
706 	td->td_retval[0] = (int)CONVTCK(tv);
707 	return 0;
708 }
709 
710 int
711 linux_newuname(struct thread *td, struct linux_newuname_args *args)
712 {
713 	struct l_new_utsname utsname;
714 	char osname[LINUX_MAX_UTSNAME];
715 	char osrelease[LINUX_MAX_UTSNAME];
716 	char *p;
717 
718 #ifdef DEBUG
719 	if (ldebug(newuname))
720 		printf(ARGS(newuname, "*"));
721 #endif
722 
723 	linux_get_osname(td, osname);
724 	linux_get_osrelease(td, osrelease);
725 
726 	bzero(&utsname, sizeof(utsname));
727 	strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
728 	getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
729 	strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
730 	strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
731 	for (p = utsname.version; *p != '\0'; ++p)
732 		if (*p == '\n') {
733 			*p = '\0';
734 			break;
735 		}
736 #ifdef __i386__
737 	{
738 		const char *class;
739 
740 		switch (cpu_class) {
741 		case CPUCLASS_686:
742 			class = "i686";
743 			break;
744 		case CPUCLASS_586:
745 			class = "i586";
746 			break;
747 		case CPUCLASS_486:
748 			class = "i486";
749 			break;
750 		default:
751 			class = "i386";
752 		}
753 		strlcpy(utsname.machine, class, LINUX_MAX_UTSNAME);
754 	}
755 #elif defined(__amd64__)	/* XXX: Linux can change 'personality'. */
756 #ifdef COMPAT_LINUX32
757 	strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME);
758 #else
759 	strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME);
760 #endif /* COMPAT_LINUX32 */
761 #else /* something other than i386 or amd64 - assume we and Linux agree */
762 	strlcpy(utsname.machine, machine, LINUX_MAX_UTSNAME);
763 #endif /* __i386__ */
764 	strlcpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME);
765 
766 	return (copyout(&utsname, args->buf, sizeof(utsname)));
767 }
768 
769 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
770 struct l_utimbuf {
771 	l_time_t l_actime;
772 	l_time_t l_modtime;
773 };
774 
775 int
776 linux_utime(struct thread *td, struct linux_utime_args *args)
777 {
778 	struct timeval tv[2], *tvp;
779 	struct l_utimbuf lut;
780 	char *fname;
781 	int error;
782 
783 	LCONVPATHEXIST(td, args->fname, &fname);
784 
785 #ifdef DEBUG
786 	if (ldebug(utime))
787 		printf(ARGS(utime, "%s, *"), fname);
788 #endif
789 
790 	if (args->times) {
791 		if ((error = copyin(args->times, &lut, sizeof lut))) {
792 			LFREEPATH(fname);
793 			return error;
794 		}
795 		tv[0].tv_sec = lut.l_actime;
796 		tv[0].tv_usec = 0;
797 		tv[1].tv_sec = lut.l_modtime;
798 		tv[1].tv_usec = 0;
799 		tvp = tv;
800 	} else
801 		tvp = NULL;
802 
803 	error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
804 	LFREEPATH(fname);
805 	return (error);
806 }
807 
808 int
809 linux_utimes(struct thread *td, struct linux_utimes_args *args)
810 {
811 	l_timeval ltv[2];
812 	struct timeval tv[2], *tvp = NULL;
813 	char *fname;
814 	int error;
815 
816 	LCONVPATHEXIST(td, args->fname, &fname);
817 
818 #ifdef DEBUG
819 	if (ldebug(utimes))
820 		printf(ARGS(utimes, "%s, *"), fname);
821 #endif
822 
823 	if (args->tptr != NULL) {
824 		if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
825 			LFREEPATH(fname);
826 			return (error);
827 		}
828 		tv[0].tv_sec = ltv[0].tv_sec;
829 		tv[0].tv_usec = ltv[0].tv_usec;
830 		tv[1].tv_sec = ltv[1].tv_sec;
831 		tv[1].tv_usec = ltv[1].tv_usec;
832 		tvp = tv;
833 	}
834 
835 	error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
836 	LFREEPATH(fname);
837 	return (error);
838 }
839 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
840 
841 #define __WCLONE 0x80000000
842 
843 int
844 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
845 {
846 	int error, options, tmpstat;
847 
848 #ifdef DEBUG
849 	if (ldebug(waitpid))
850 		printf(ARGS(waitpid, "%d, %p, %d"),
851 		    args->pid, (void *)args->status, args->options);
852 #endif
853 	/*
854 	 * this is necessary because the test in kern_wait doesn't work
855 	 * because we mess with the options here
856 	 */
857 	if (args->options & ~(WUNTRACED | WNOHANG | WCONTINUED | __WCLONE))
858 		return (EINVAL);
859 
860 	options = (args->options & (WNOHANG | WUNTRACED));
861 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
862 	if (args->options & __WCLONE)
863 		options |= WLINUXCLONE;
864 
865 	error = kern_wait(td, args->pid, &tmpstat, options, NULL);
866 	if (error)
867 		return error;
868 
869 	if (args->status) {
870 		tmpstat &= 0xffff;
871 		if (WIFSIGNALED(tmpstat))
872 			tmpstat = (tmpstat & 0xffffff80) |
873 			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
874 		else if (WIFSTOPPED(tmpstat))
875 			tmpstat = (tmpstat & 0xffff00ff) |
876 			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
877 		return copyout(&tmpstat, args->status, sizeof(int));
878 	}
879 
880 	return 0;
881 }
882 
883 int
884 linux_wait4(struct thread *td, struct linux_wait4_args *args)
885 {
886 	int error, options, tmpstat;
887 	struct rusage ru, *rup;
888 	struct proc *p;
889 
890 #ifdef DEBUG
891 	if (ldebug(wait4))
892 		printf(ARGS(wait4, "%d, %p, %d, %p"),
893 		    args->pid, (void *)args->status, args->options,
894 		    (void *)args->rusage);
895 #endif
896 
897 	options = (args->options & (WNOHANG | WUNTRACED));
898 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
899 	if (args->options & __WCLONE)
900 		options |= WLINUXCLONE;
901 
902 	if (args->rusage != NULL)
903 		rup = &ru;
904 	else
905 		rup = NULL;
906 	error = kern_wait(td, args->pid, &tmpstat, options, rup);
907 	if (error)
908 		return error;
909 
910 	p = td->td_proc;
911 	PROC_LOCK(p);
912 	sigqueue_delete(&p->p_sigqueue, SIGCHLD);
913 	PROC_UNLOCK(p);
914 
915 	if (args->status) {
916 		tmpstat &= 0xffff;
917 		if (WIFSIGNALED(tmpstat))
918 			tmpstat = (tmpstat & 0xffffff80) |
919 			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
920 		else if (WIFSTOPPED(tmpstat))
921 			tmpstat = (tmpstat & 0xffff00ff) |
922 			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
923 		error = copyout(&tmpstat, args->status, sizeof(int));
924 	}
925 	if (args->rusage != NULL && error == 0)
926 		error = copyout(&ru, args->rusage, sizeof(ru));
927 
928 	return (error);
929 }
930 
931 int
932 linux_mknod(struct thread *td, struct linux_mknod_args *args)
933 {
934 	char *path;
935 	int error;
936 
937 	LCONVPATHCREAT(td, args->path, &path);
938 
939 #ifdef DEBUG
940 	if (ldebug(mknod))
941 		printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
942 #endif
943 
944 	switch (args->mode & S_IFMT) {
945 	case S_IFIFO:
946 	case S_IFSOCK:
947 		error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
948 		break;
949 
950 	case S_IFCHR:
951 	case S_IFBLK:
952 		error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
953 		    args->dev);
954 		break;
955 
956 	case S_IFDIR:
957 		error = EPERM;
958 		break;
959 
960 	case 0:
961 		args->mode |= S_IFREG;
962 		/* FALLTHROUGH */
963 	case S_IFREG:
964 		error = kern_open(td, path, UIO_SYSSPACE,
965 		    O_WRONLY | O_CREAT | O_TRUNC, args->mode);
966 		break;
967 
968 	default:
969 		error = EINVAL;
970 		break;
971 	}
972 	LFREEPATH(path);
973 	return (error);
974 }
975 
976 /*
977  * UGH! This is just about the dumbest idea I've ever heard!!
978  */
979 int
980 linux_personality(struct thread *td, struct linux_personality_args *args)
981 {
982 #ifdef DEBUG
983 	if (ldebug(personality))
984 		printf(ARGS(personality, "%lu"), (unsigned long)args->per);
985 #endif
986 	if (args->per != 0)
987 		return EINVAL;
988 
989 	/* Yes Jim, it's still a Linux... */
990 	td->td_retval[0] = 0;
991 	return 0;
992 }
993 
994 struct l_itimerval {
995 	l_timeval it_interval;
996 	l_timeval it_value;
997 };
998 
999 #define	B2L_ITIMERVAL(bip, lip) 					\
1000 	(bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec;		\
1001 	(bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec;	\
1002 	(bip)->it_value.tv_sec = (lip)->it_value.tv_sec;		\
1003 	(bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1004 
1005 int
1006 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1007 {
1008 	int error;
1009 	struct l_itimerval ls;
1010 	struct itimerval aitv, oitv;
1011 
1012 #ifdef DEBUG
1013 	if (ldebug(setitimer))
1014 		printf(ARGS(setitimer, "%p, %p"),
1015 		    (void *)uap->itv, (void *)uap->oitv);
1016 #endif
1017 
1018 	if (uap->itv == NULL) {
1019 		uap->itv = uap->oitv;
1020 		return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1021 	}
1022 
1023 	error = copyin(uap->itv, &ls, sizeof(ls));
1024 	if (error != 0)
1025 		return (error);
1026 	B2L_ITIMERVAL(&aitv, &ls);
1027 #ifdef DEBUG
1028 	if (ldebug(setitimer)) {
1029 		printf("setitimer: value: sec: %jd, usec: %ld\n",
1030 		    (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1031 		printf("setitimer: interval: sec: %jd, usec: %ld\n",
1032 		    (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1033 	}
1034 #endif
1035 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
1036 	if (error != 0 || uap->oitv == NULL)
1037 		return (error);
1038 	B2L_ITIMERVAL(&ls, &oitv);
1039 
1040 	return (copyout(&ls, uap->oitv, sizeof(ls)));
1041 }
1042 
1043 int
1044 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1045 {
1046 	int error;
1047 	struct l_itimerval ls;
1048 	struct itimerval aitv;
1049 
1050 #ifdef DEBUG
1051 	if (ldebug(getitimer))
1052 		printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1053 #endif
1054 	error = kern_getitimer(td, uap->which, &aitv);
1055 	if (error != 0)
1056 		return (error);
1057 	B2L_ITIMERVAL(&ls, &aitv);
1058 	return (copyout(&ls, uap->itv, sizeof(ls)));
1059 }
1060 
1061 int
1062 linux_nice(struct thread *td, struct linux_nice_args *args)
1063 {
1064 	struct setpriority_args bsd_args;
1065 
1066 	bsd_args.which = PRIO_PROCESS;
1067 	bsd_args.who = 0;		/* current process */
1068 	bsd_args.prio = args->inc;
1069 	return setpriority(td, &bsd_args);
1070 }
1071 
1072 int
1073 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1074 {
1075 	struct ucred *newcred, *oldcred;
1076 	l_gid_t linux_gidset[NGROUPS];
1077 	gid_t *bsd_gidset;
1078 	int ngrp, error;
1079 	struct proc *p;
1080 
1081 	ngrp = args->gidsetsize;
1082 	if (ngrp < 0 || ngrp >= NGROUPS)
1083 		return (EINVAL);
1084 	error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1085 	if (error)
1086 		return (error);
1087 	newcred = crget();
1088 	p = td->td_proc;
1089 	PROC_LOCK(p);
1090 	oldcred = p->p_ucred;
1091 
1092 	/*
1093 	 * cr_groups[0] holds egid. Setting the whole set from
1094 	 * the supplied set will cause egid to be changed too.
1095 	 * Keep cr_groups[0] unchanged to prevent that.
1096 	 */
1097 
1098 	if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
1099 		PROC_UNLOCK(p);
1100 		crfree(newcred);
1101 		return (error);
1102 	}
1103 
1104 	crcopy(newcred, oldcred);
1105 	if (ngrp > 0) {
1106 		newcred->cr_ngroups = ngrp + 1;
1107 
1108 		bsd_gidset = newcred->cr_groups;
1109 		ngrp--;
1110 		while (ngrp >= 0) {
1111 			bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1112 			ngrp--;
1113 		}
1114 	} else
1115 		newcred->cr_ngroups = 1;
1116 
1117 	setsugid(p);
1118 	p->p_ucred = newcred;
1119 	PROC_UNLOCK(p);
1120 	crfree(oldcred);
1121 	return (0);
1122 }
1123 
1124 int
1125 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1126 {
1127 	struct ucred *cred;
1128 	l_gid_t linux_gidset[NGROUPS];
1129 	gid_t *bsd_gidset;
1130 	int bsd_gidsetsz, ngrp, error;
1131 
1132 	cred = td->td_ucred;
1133 	bsd_gidset = cred->cr_groups;
1134 	bsd_gidsetsz = cred->cr_ngroups - 1;
1135 
1136 	/*
1137 	 * cr_groups[0] holds egid. Returning the whole set
1138 	 * here will cause a duplicate. Exclude cr_groups[0]
1139 	 * to prevent that.
1140 	 */
1141 
1142 	if ((ngrp = args->gidsetsize) == 0) {
1143 		td->td_retval[0] = bsd_gidsetsz;
1144 		return (0);
1145 	}
1146 
1147 	if (ngrp < bsd_gidsetsz)
1148 		return (EINVAL);
1149 
1150 	ngrp = 0;
1151 	while (ngrp < bsd_gidsetsz) {
1152 		linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1153 		ngrp++;
1154 	}
1155 
1156 	if ((error = copyout(linux_gidset, args->grouplist,
1157 	    ngrp * sizeof(l_gid_t))))
1158 		return (error);
1159 
1160 	td->td_retval[0] = ngrp;
1161 	return (0);
1162 }
1163 
1164 int
1165 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1166 {
1167 	struct rlimit bsd_rlim;
1168 	struct l_rlimit rlim;
1169 	u_int which;
1170 	int error;
1171 
1172 #ifdef DEBUG
1173 	if (ldebug(setrlimit))
1174 		printf(ARGS(setrlimit, "%d, %p"),
1175 		    args->resource, (void *)args->rlim);
1176 #endif
1177 
1178 	if (args->resource >= LINUX_RLIM_NLIMITS)
1179 		return (EINVAL);
1180 
1181 	which = linux_to_bsd_resource[args->resource];
1182 	if (which == -1)
1183 		return (EINVAL);
1184 
1185 	error = copyin(args->rlim, &rlim, sizeof(rlim));
1186 	if (error)
1187 		return (error);
1188 
1189 	bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1190 	bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1191 	return (kern_setrlimit(td, which, &bsd_rlim));
1192 }
1193 
1194 int
1195 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1196 {
1197 	struct l_rlimit rlim;
1198 	struct proc *p = td->td_proc;
1199 	struct rlimit bsd_rlim;
1200 	u_int which;
1201 
1202 #ifdef DEBUG
1203 	if (ldebug(old_getrlimit))
1204 		printf(ARGS(old_getrlimit, "%d, %p"),
1205 		    args->resource, (void *)args->rlim);
1206 #endif
1207 
1208 	if (args->resource >= LINUX_RLIM_NLIMITS)
1209 		return (EINVAL);
1210 
1211 	which = linux_to_bsd_resource[args->resource];
1212 	if (which == -1)
1213 		return (EINVAL);
1214 
1215 	PROC_LOCK(p);
1216 	lim_rlimit(p, which, &bsd_rlim);
1217 	PROC_UNLOCK(p);
1218 
1219 #ifdef COMPAT_LINUX32
1220 	rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1221 	if (rlim.rlim_cur == UINT_MAX)
1222 		rlim.rlim_cur = INT_MAX;
1223 	rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1224 	if (rlim.rlim_max == UINT_MAX)
1225 		rlim.rlim_max = INT_MAX;
1226 #else
1227 	rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1228 	if (rlim.rlim_cur == ULONG_MAX)
1229 		rlim.rlim_cur = LONG_MAX;
1230 	rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1231 	if (rlim.rlim_max == ULONG_MAX)
1232 		rlim.rlim_max = LONG_MAX;
1233 #endif
1234 	return (copyout(&rlim, args->rlim, sizeof(rlim)));
1235 }
1236 
1237 int
1238 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1239 {
1240 	struct l_rlimit rlim;
1241 	struct proc *p = td->td_proc;
1242 	struct rlimit bsd_rlim;
1243 	u_int which;
1244 
1245 #ifdef DEBUG
1246 	if (ldebug(getrlimit))
1247 		printf(ARGS(getrlimit, "%d, %p"),
1248 		    args->resource, (void *)args->rlim);
1249 #endif
1250 
1251 	if (args->resource >= LINUX_RLIM_NLIMITS)
1252 		return (EINVAL);
1253 
1254 	which = linux_to_bsd_resource[args->resource];
1255 	if (which == -1)
1256 		return (EINVAL);
1257 
1258 	PROC_LOCK(p);
1259 	lim_rlimit(p, which, &bsd_rlim);
1260 	PROC_UNLOCK(p);
1261 
1262 	rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1263 	rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1264 	return (copyout(&rlim, args->rlim, sizeof(rlim)));
1265 }
1266 
1267 int
1268 linux_sched_setscheduler(struct thread *td,
1269     struct linux_sched_setscheduler_args *args)
1270 {
1271 	struct sched_setscheduler_args bsd;
1272 
1273 #ifdef DEBUG
1274 	if (ldebug(sched_setscheduler))
1275 		printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1276 		    args->pid, args->policy, (const void *)args->param);
1277 #endif
1278 
1279 	switch (args->policy) {
1280 	case LINUX_SCHED_OTHER:
1281 		bsd.policy = SCHED_OTHER;
1282 		break;
1283 	case LINUX_SCHED_FIFO:
1284 		bsd.policy = SCHED_FIFO;
1285 		break;
1286 	case LINUX_SCHED_RR:
1287 		bsd.policy = SCHED_RR;
1288 		break;
1289 	default:
1290 		return EINVAL;
1291 	}
1292 
1293 	bsd.pid = args->pid;
1294 	bsd.param = (struct sched_param *)args->param;
1295 	return sched_setscheduler(td, &bsd);
1296 }
1297 
1298 int
1299 linux_sched_getscheduler(struct thread *td,
1300     struct linux_sched_getscheduler_args *args)
1301 {
1302 	struct sched_getscheduler_args bsd;
1303 	int error;
1304 
1305 #ifdef DEBUG
1306 	if (ldebug(sched_getscheduler))
1307 		printf(ARGS(sched_getscheduler, "%d"), args->pid);
1308 #endif
1309 
1310 	bsd.pid = args->pid;
1311 	error = sched_getscheduler(td, &bsd);
1312 
1313 	switch (td->td_retval[0]) {
1314 	case SCHED_OTHER:
1315 		td->td_retval[0] = LINUX_SCHED_OTHER;
1316 		break;
1317 	case SCHED_FIFO:
1318 		td->td_retval[0] = LINUX_SCHED_FIFO;
1319 		break;
1320 	case SCHED_RR:
1321 		td->td_retval[0] = LINUX_SCHED_RR;
1322 		break;
1323 	}
1324 
1325 	return error;
1326 }
1327 
1328 int
1329 linux_sched_get_priority_max(struct thread *td,
1330     struct linux_sched_get_priority_max_args *args)
1331 {
1332 	struct sched_get_priority_max_args bsd;
1333 
1334 #ifdef DEBUG
1335 	if (ldebug(sched_get_priority_max))
1336 		printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1337 #endif
1338 
1339 	switch (args->policy) {
1340 	case LINUX_SCHED_OTHER:
1341 		bsd.policy = SCHED_OTHER;
1342 		break;
1343 	case LINUX_SCHED_FIFO:
1344 		bsd.policy = SCHED_FIFO;
1345 		break;
1346 	case LINUX_SCHED_RR:
1347 		bsd.policy = SCHED_RR;
1348 		break;
1349 	default:
1350 		return EINVAL;
1351 	}
1352 	return sched_get_priority_max(td, &bsd);
1353 }
1354 
1355 int
1356 linux_sched_get_priority_min(struct thread *td,
1357     struct linux_sched_get_priority_min_args *args)
1358 {
1359 	struct sched_get_priority_min_args bsd;
1360 
1361 #ifdef DEBUG
1362 	if (ldebug(sched_get_priority_min))
1363 		printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1364 #endif
1365 
1366 	switch (args->policy) {
1367 	case LINUX_SCHED_OTHER:
1368 		bsd.policy = SCHED_OTHER;
1369 		break;
1370 	case LINUX_SCHED_FIFO:
1371 		bsd.policy = SCHED_FIFO;
1372 		break;
1373 	case LINUX_SCHED_RR:
1374 		bsd.policy = SCHED_RR;
1375 		break;
1376 	default:
1377 		return EINVAL;
1378 	}
1379 	return sched_get_priority_min(td, &bsd);
1380 }
1381 
1382 #define REBOOT_CAD_ON	0x89abcdef
1383 #define REBOOT_CAD_OFF	0
1384 #define REBOOT_HALT	0xcdef0123
1385 #define REBOOT_RESTART	0x01234567
1386 #define REBOOT_RESTART2	0xA1B2C3D4
1387 #define REBOOT_POWEROFF	0x4321FEDC
1388 #define REBOOT_MAGIC1	0xfee1dead
1389 #define REBOOT_MAGIC2	0x28121969
1390 #define REBOOT_MAGIC2A	0x05121996
1391 #define REBOOT_MAGIC2B	0x16041998
1392 
1393 int
1394 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1395 {
1396 	struct reboot_args bsd_args;
1397 
1398 #ifdef DEBUG
1399 	if (ldebug(reboot))
1400 		printf(ARGS(reboot, "0x%x"), args->cmd);
1401 #endif
1402 
1403 	if (args->magic1 != REBOOT_MAGIC1)
1404 		return EINVAL;
1405 
1406 	switch (args->magic2) {
1407 	case REBOOT_MAGIC2:
1408 	case REBOOT_MAGIC2A:
1409 	case REBOOT_MAGIC2B:
1410 		break;
1411 	default:
1412 		return EINVAL;
1413 	}
1414 
1415 	switch (args->cmd) {
1416 	case REBOOT_CAD_ON:
1417 	case REBOOT_CAD_OFF:
1418 		return (priv_check(td, PRIV_REBOOT));
1419 	case REBOOT_HALT:
1420 		bsd_args.opt = RB_HALT;
1421 		break;
1422 	case REBOOT_RESTART:
1423 	case REBOOT_RESTART2:
1424 		bsd_args.opt = 0;
1425 		break;
1426 	case REBOOT_POWEROFF:
1427 		bsd_args.opt = RB_POWEROFF;
1428 		break;
1429 	default:
1430 		return EINVAL;
1431 	}
1432 	return reboot(td, &bsd_args);
1433 }
1434 
1435 
1436 /*
1437  * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1438  * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1439  * are assumed to be preserved. The following lightweight syscalls fixes
1440  * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1441  *
1442  * linux_getpid() - MP SAFE
1443  * linux_getgid() - MP SAFE
1444  * linux_getuid() - MP SAFE
1445  */
1446 
1447 int
1448 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1449 {
1450 	struct linux_emuldata *em;
1451 
1452 #ifdef DEBUG
1453 	if (ldebug(getpid))
1454 		printf(ARGS(getpid, ""));
1455 #endif
1456 
1457 	if (linux_use26(td)) {
1458 		em = em_find(td->td_proc, EMUL_DONTLOCK);
1459 		KASSERT(em != NULL, ("getpid: emuldata not found.\n"));
1460 		td->td_retval[0] = em->shared->group_pid;
1461 	} else {
1462 		td->td_retval[0] = td->td_proc->p_pid;
1463 	}
1464 
1465 	return (0);
1466 }
1467 
1468 int
1469 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1470 {
1471 
1472 #ifdef DEBUG
1473 	if (ldebug(gettid))
1474 		printf(ARGS(gettid, ""));
1475 #endif
1476 
1477 	td->td_retval[0] = td->td_proc->p_pid;
1478 	return (0);
1479 }
1480 
1481 
1482 int
1483 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1484 {
1485 	struct linux_emuldata *em;
1486 	struct proc *p, *pp;
1487 
1488 #ifdef DEBUG
1489 	if (ldebug(getppid))
1490 		printf(ARGS(getppid, ""));
1491 #endif
1492 
1493 	if (!linux_use26(td)) {
1494 		PROC_LOCK(td->td_proc);
1495 		td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1496 		PROC_UNLOCK(td->td_proc);
1497 		return (0);
1498 	}
1499 
1500 	em = em_find(td->td_proc, EMUL_DONTLOCK);
1501 
1502 	KASSERT(em != NULL, ("getppid: process emuldata not found.\n"));
1503 
1504 	/* find the group leader */
1505 	p = pfind(em->shared->group_pid);
1506 
1507 	if (p == NULL) {
1508 #ifdef DEBUG
1509 	   	printf(LMSG("parent process not found.\n"));
1510 #endif
1511 		return (0);
1512 	}
1513 
1514 	pp = p->p_pptr;		/* switch to parent */
1515 	PROC_LOCK(pp);
1516 	PROC_UNLOCK(p);
1517 
1518 	/* if its also linux process */
1519 	if (pp->p_sysent == &elf_linux_sysvec) {
1520 		em = em_find(pp, EMUL_DONTLOCK);
1521 		KASSERT(em != NULL, ("getppid: parent emuldata not found.\n"));
1522 
1523 		td->td_retval[0] = em->shared->group_pid;
1524 	} else
1525 		td->td_retval[0] = pp->p_pid;
1526 
1527 	PROC_UNLOCK(pp);
1528 
1529 	return (0);
1530 }
1531 
1532 int
1533 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1534 {
1535 
1536 #ifdef DEBUG
1537 	if (ldebug(getgid))
1538 		printf(ARGS(getgid, ""));
1539 #endif
1540 
1541 	td->td_retval[0] = td->td_ucred->cr_rgid;
1542 	return (0);
1543 }
1544 
1545 int
1546 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1547 {
1548 
1549 #ifdef DEBUG
1550 	if (ldebug(getuid))
1551 		printf(ARGS(getuid, ""));
1552 #endif
1553 
1554 	td->td_retval[0] = td->td_ucred->cr_ruid;
1555 	return (0);
1556 }
1557 
1558 
1559 int
1560 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1561 {
1562 	struct getsid_args bsd;
1563 
1564 #ifdef DEBUG
1565 	if (ldebug(getsid))
1566 		printf(ARGS(getsid, "%i"), args->pid);
1567 #endif
1568 
1569 	bsd.pid = args->pid;
1570 	return getsid(td, &bsd);
1571 }
1572 
1573 int
1574 linux_nosys(struct thread *td, struct nosys_args *ignore)
1575 {
1576 
1577 	return (ENOSYS);
1578 }
1579 
1580 int
1581 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1582 {
1583 	struct getpriority_args bsd_args;
1584 	int error;
1585 
1586 #ifdef DEBUG
1587 	if (ldebug(getpriority))
1588 		printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1589 #endif
1590 
1591 	bsd_args.which = args->which;
1592 	bsd_args.who = args->who;
1593 	error = getpriority(td, &bsd_args);
1594 	td->td_retval[0] = 20 - td->td_retval[0];
1595 	return error;
1596 }
1597 
1598 int
1599 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1600 {
1601 	int name[2];
1602 
1603 #ifdef DEBUG
1604 	if (ldebug(sethostname))
1605 		printf(ARGS(sethostname, "*, %i"), args->len);
1606 #endif
1607 
1608 	name[0] = CTL_KERN;
1609 	name[1] = KERN_HOSTNAME;
1610 	return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1611 	    args->len, 0, 0));
1612 }
1613 
1614 int
1615 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1616 {
1617 	struct linux_emuldata *em, *td_em, *tmp_em;
1618 	struct proc *sp;
1619 
1620 #ifdef DEBUG
1621 	if (ldebug(exit_group))
1622 		printf(ARGS(exit_group, "%i"), args->error_code);
1623 #endif
1624 
1625 	if (linux_use26(td)) {
1626 		td_em = em_find(td->td_proc, EMUL_DONTLOCK);
1627 
1628 		KASSERT(td_em != NULL, ("exit_group: emuldata not found.\n"));
1629 
1630 		EMUL_SHARED_RLOCK(&emul_shared_lock);
1631 		LIST_FOREACH_SAFE(em, &td_em->shared->threads, threads, tmp_em) {
1632 			if (em->pid == td_em->pid)
1633 				continue;
1634 
1635 			sp = pfind(em->pid);
1636 			psignal(sp, SIGKILL);
1637 			PROC_UNLOCK(sp);
1638 #ifdef DEBUG
1639 			printf(LMSG("linux_sys_exit_group: kill PID %d\n"), em->pid);
1640 #endif
1641 		}
1642 
1643 		EMUL_SHARED_RUNLOCK(&emul_shared_lock);
1644 	}
1645 	/*
1646 	 * XXX: we should send a signal to the parent if
1647 	 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1648 	 * as it doesnt occur often.
1649 	 */
1650 	exit1(td, W_EXITCODE(args->error_code, 0));
1651 
1652 	return (0);
1653 }
1654 
1655 int
1656 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1657 {
1658 	int error = 0, max_size;
1659 	struct proc *p = td->td_proc;
1660 	char comm[LINUX_MAX_COMM_LEN];
1661 	struct linux_emuldata *em;
1662 	int pdeath_signal;
1663 
1664 #ifdef DEBUG
1665 	if (ldebug(prctl))
1666 		printf(ARGS(prctl, "%d, %d, %d, %d, %d"), args->option,
1667 		    args->arg2, args->arg3, args->arg4, args->arg5);
1668 #endif
1669 
1670 	switch (args->option) {
1671 	case LINUX_PR_SET_PDEATHSIG:
1672 		if (!LINUX_SIG_VALID(args->arg2))
1673 			return (EINVAL);
1674 		em = em_find(p, EMUL_DOLOCK);
1675 		KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1676 		em->pdeath_signal = args->arg2;
1677 		EMUL_UNLOCK(&emul_lock);
1678 		break;
1679 	case LINUX_PR_GET_PDEATHSIG:
1680 		em = em_find(p, EMUL_DOLOCK);
1681 		KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1682 		pdeath_signal = em->pdeath_signal;
1683 		EMUL_UNLOCK(&emul_lock);
1684 		error = copyout(&pdeath_signal,
1685 		    (void *)(register_t)args->arg2,
1686 		    sizeof(pdeath_signal));
1687 		break;
1688 	case LINUX_PR_SET_NAME:
1689 		/*
1690 		 * To be on the safe side we need to make sure to not
1691 		 * overflow the size a linux program expects. We already
1692 		 * do this here in the copyin, so that we don't need to
1693 		 * check on copyout.
1694 		 */
1695 		max_size = MIN(sizeof(comm), sizeof(p->p_comm));
1696 		error = copyinstr((void *)(register_t)args->arg2, comm,
1697 		    max_size, NULL);
1698 
1699 		/* Linux silently truncates the name if it is too long. */
1700 		if (error == ENAMETOOLONG) {
1701 			/*
1702 			 * XXX: copyinstr() isn't documented to populate the
1703 			 * array completely, so do a copyin() to be on the
1704 			 * safe side. This should be changed in case
1705 			 * copyinstr() is changed to guarantee this.
1706 			 */
1707 			error = copyin((void *)(register_t)args->arg2, comm,
1708 			    max_size - 1);
1709 			comm[max_size - 1] = '\0';
1710 		}
1711 		if (error)
1712 			return (error);
1713 
1714 		PROC_LOCK(p);
1715 		strlcpy(p->p_comm, comm, sizeof(p->p_comm));
1716 		PROC_UNLOCK(p);
1717 		break;
1718 	case LINUX_PR_GET_NAME:
1719 		PROC_LOCK(p);
1720 		strlcpy(comm, p->p_comm, sizeof(comm));
1721 		PROC_UNLOCK(p);
1722 		error = copyout(comm, (void *)(register_t)args->arg2,
1723 		    strlen(comm) + 1);
1724 		break;
1725 	default:
1726 		error = EINVAL;
1727 		break;
1728 	}
1729 
1730 	return (error);
1731 }
1732 
1733 /*
1734  * Get affinity of a process.
1735  */
1736 int
1737 linux_sched_getaffinity(struct thread *td,
1738     struct linux_sched_getaffinity_args *args)
1739 {
1740 	int error;
1741 	struct cpuset_getaffinity_args cga;
1742 
1743 #ifdef DEBUG
1744 	if (ldebug(sched_getaffinity))
1745 		printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
1746 		    args->len);
1747 #endif
1748 
1749 	cga.level = CPU_LEVEL_WHICH;
1750 	cga.which = CPU_WHICH_PID;
1751 	cga.id = args->pid;
1752 	cga.cpusetsize = sizeof(cpumask_t);
1753 	cga.mask = (cpuset_t *) args->user_mask_ptr;
1754 
1755 	if ((error = cpuset_getaffinity(td, &cga)) == 0)
1756 		td->td_retval[0] = sizeof(cpumask_t);
1757 
1758 	return (error);
1759 }
1760 
1761 /*
1762  *  Set affinity of a process.
1763  */
1764 int
1765 linux_sched_setaffinity(struct thread *td,
1766     struct linux_sched_setaffinity_args *args)
1767 {
1768 	struct cpuset_setaffinity_args csa;
1769 
1770 #ifdef DEBUG
1771 	if (ldebug(sched_setaffinity))
1772 		printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
1773 		    args->len);
1774 #endif
1775 	csa.level = CPU_LEVEL_WHICH;
1776 	csa.which = CPU_WHICH_PID;
1777 	csa.id = args->pid;
1778 	csa.cpusetsize = args->len;
1779 	csa.mask = (cpuset_t *) args->user_mask_ptr;
1780 
1781 	return (cpuset_setaffinity(td, &csa));
1782 }
1783