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