xref: /freebsd/sys/compat/linux/linux_misc.c (revision 05b432d2d18bddd0672b3cde6aad1680c174321f)
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/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 <security/mac/mac_framework.h>
67 
68 #include <vm/vm.h>
69 #include <vm/pmap.h>
70 #include <vm/vm_kern.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_extern.h>
73 #include <vm/vm_object.h>
74 #include <vm/swap_pager.h>
75 
76 #include <posix4/sched.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, -1
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_ulong		totalbig;
119 	l_ulong		freebig;
120 	l_uint		mem_unit;
121 	char		_f[6];		/* Pads structure to 64 bytes */
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 		switch (cpu_class) {
722 		case CPUCLASS_686:
723 			class = "i686";
724 			break;
725 		case CPUCLASS_586:
726 			class = "i586";
727 			break;
728 		case CPUCLASS_486:
729 			class = "i486";
730 			break;
731 		default:
732 			class = "i386";
733 		}
734 		strlcpy(utsname.machine, class, LINUX_MAX_UTSNAME);
735 	}
736 #elif defined(__amd64__)	/* XXX: Linux can change 'personality'. */
737 #ifdef COMPAT_LINUX32
738 	strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME);
739 #else
740 	strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME);
741 #endif /* COMPAT_LINUX32 */
742 #else /* something other than i386 or amd64 - assume we and Linux agree */
743 	strlcpy(utsname.machine, machine, LINUX_MAX_UTSNAME);
744 #endif /* __i386__ */
745 	strlcpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME);
746 
747 	return (copyout(&utsname, args->buf, sizeof(utsname)));
748 }
749 
750 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
751 struct l_utimbuf {
752 	l_time_t l_actime;
753 	l_time_t l_modtime;
754 };
755 
756 int
757 linux_utime(struct thread *td, struct linux_utime_args *args)
758 {
759 	struct timeval tv[2], *tvp;
760 	struct l_utimbuf lut;
761 	char *fname;
762 	int error;
763 
764 	LCONVPATHEXIST(td, args->fname, &fname);
765 
766 #ifdef DEBUG
767 	if (ldebug(utime))
768 		printf(ARGS(utime, "%s, *"), fname);
769 #endif
770 
771 	if (args->times) {
772 		if ((error = copyin(args->times, &lut, sizeof lut))) {
773 			LFREEPATH(fname);
774 			return error;
775 		}
776 		tv[0].tv_sec = lut.l_actime;
777 		tv[0].tv_usec = 0;
778 		tv[1].tv_sec = lut.l_modtime;
779 		tv[1].tv_usec = 0;
780 		tvp = tv;
781 	} else
782 		tvp = NULL;
783 
784 	error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
785 	LFREEPATH(fname);
786 	return (error);
787 }
788 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
789 
790 #define __WCLONE 0x80000000
791 
792 int
793 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
794 {
795 	int error, options, tmpstat;
796 
797 #ifdef DEBUG
798 	if (ldebug(waitpid))
799 		printf(ARGS(waitpid, "%d, %p, %d"),
800 		    args->pid, (void *)args->status, args->options);
801 #endif
802 	/*
803 	 * this is necessary because the test in kern_wait doesnt
804 	 * work because we mess with the options here
805 	 */
806 	if (args->options &~ (WUNTRACED|WNOHANG|WCONTINUED))
807 	   	return (EINVAL);
808 
809 	options = (args->options & (WNOHANG | WUNTRACED));
810 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
811 	if (args->options & __WCLONE)
812 		options |= WLINUXCLONE;
813 
814 	error = kern_wait(td, args->pid, &tmpstat, options, NULL);
815 	if (error)
816 		return error;
817 
818 	if (args->status) {
819 		tmpstat &= 0xffff;
820 		if (WIFSIGNALED(tmpstat))
821 			tmpstat = (tmpstat & 0xffffff80) |
822 			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
823 		else if (WIFSTOPPED(tmpstat))
824 			tmpstat = (tmpstat & 0xffff00ff) |
825 			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
826 		return copyout(&tmpstat, args->status, sizeof(int));
827 	}
828 
829 	return 0;
830 }
831 
832 int
833 linux_wait4(struct thread *td, struct linux_wait4_args *args)
834 {
835 	int error, options, tmpstat;
836 	struct rusage ru, *rup;
837 	struct proc *p;
838 
839 #ifdef DEBUG
840 	if (ldebug(wait4))
841 		printf(ARGS(wait4, "%d, %p, %d, %p"),
842 		    args->pid, (void *)args->status, args->options,
843 		    (void *)args->rusage);
844 #endif
845 
846 	options = (args->options & (WNOHANG | WUNTRACED));
847 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
848 	if (args->options & __WCLONE)
849 		options |= WLINUXCLONE;
850 
851 	if (args->rusage != NULL)
852 		rup = &ru;
853 	else
854 		rup = NULL;
855 	error = kern_wait(td, args->pid, &tmpstat, options, rup);
856 	if (error)
857 		return error;
858 
859 	p = td->td_proc;
860 	PROC_LOCK(p);
861 	sigqueue_delete(&p->p_sigqueue, SIGCHLD);
862 	PROC_UNLOCK(p);
863 
864 	if (args->status) {
865 		tmpstat &= 0xffff;
866 		if (WIFSIGNALED(tmpstat))
867 			tmpstat = (tmpstat & 0xffffff80) |
868 			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
869 		else if (WIFSTOPPED(tmpstat))
870 			tmpstat = (tmpstat & 0xffff00ff) |
871 			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
872 		error = copyout(&tmpstat, args->status, sizeof(int));
873 	}
874 	if (args->rusage != NULL && error == 0)
875 		error = copyout(&ru, args->rusage, sizeof(ru));
876 
877 	return (error);
878 }
879 
880 int
881 linux_mknod(struct thread *td, struct linux_mknod_args *args)
882 {
883 	char *path;
884 	int error;
885 
886 	LCONVPATHCREAT(td, args->path, &path);
887 
888 #ifdef DEBUG
889 	if (ldebug(mknod))
890 		printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
891 #endif
892 
893 	if (S_ISFIFO(args->mode))
894 		error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
895 	else
896 		error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
897 		    args->dev);
898 	LFREEPATH(path);
899 	return (error);
900 }
901 
902 /*
903  * UGH! This is just about the dumbest idea I've ever heard!!
904  */
905 int
906 linux_personality(struct thread *td, struct linux_personality_args *args)
907 {
908 #ifdef DEBUG
909 	if (ldebug(personality))
910 		printf(ARGS(personality, "%lu"), (unsigned long)args->per);
911 #endif
912 	if (args->per != 0)
913 		return EINVAL;
914 
915 	/* Yes Jim, it's still a Linux... */
916 	td->td_retval[0] = 0;
917 	return 0;
918 }
919 
920 struct l_itimerval {
921 	l_timeval it_interval;
922 	l_timeval it_value;
923 };
924 
925 #define	B2L_ITIMERVAL(bip, lip) 					\
926 	(bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec;		\
927 	(bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec;	\
928 	(bip)->it_value.tv_sec = (lip)->it_value.tv_sec;		\
929 	(bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
930 
931 int
932 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
933 {
934 	int error;
935 	struct l_itimerval ls;
936 	struct itimerval aitv, oitv;
937 
938 #ifdef DEBUG
939 	if (ldebug(setitimer))
940 		printf(ARGS(setitimer, "%p, %p"),
941 		    (void *)uap->itv, (void *)uap->oitv);
942 #endif
943 
944 	if (uap->itv == NULL) {
945 		uap->itv = uap->oitv;
946 		return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
947 	}
948 
949 	error = copyin(uap->itv, &ls, sizeof(ls));
950 	if (error != 0)
951 		return (error);
952 	B2L_ITIMERVAL(&aitv, &ls);
953 #ifdef DEBUG
954 	if (ldebug(setitimer)) {
955 		printf("setitimer: value: sec: %jd, usec: %ld\n",
956 		    (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
957 		printf("setitimer: interval: sec: %jd, usec: %ld\n",
958 		    (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
959 	}
960 #endif
961 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
962 	if (error != 0 || uap->oitv == NULL)
963 		return (error);
964 	B2L_ITIMERVAL(&ls, &oitv);
965 
966 	return (copyout(&ls, uap->oitv, sizeof(ls)));
967 }
968 
969 int
970 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
971 {
972 	int error;
973 	struct l_itimerval ls;
974 	struct itimerval aitv;
975 
976 #ifdef DEBUG
977 	if (ldebug(getitimer))
978 		printf(ARGS(getitimer, "%p"), (void *)uap->itv);
979 #endif
980 	error = kern_getitimer(td, uap->which, &aitv);
981 	if (error != 0)
982 		return (error);
983 	B2L_ITIMERVAL(&ls, &aitv);
984 	return (copyout(&ls, uap->itv, sizeof(ls)));
985 }
986 
987 int
988 linux_nice(struct thread *td, struct linux_nice_args *args)
989 {
990 	struct setpriority_args	bsd_args;
991 
992 	bsd_args.which = PRIO_PROCESS;
993 	bsd_args.who = 0;	/* current process */
994 	bsd_args.prio = args->inc;
995 	return setpriority(td, &bsd_args);
996 }
997 
998 int
999 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1000 {
1001 	struct ucred *newcred, *oldcred;
1002 	l_gid_t linux_gidset[NGROUPS];
1003 	gid_t *bsd_gidset;
1004 	int ngrp, error;
1005 	struct proc *p;
1006 
1007 	ngrp = args->gidsetsize;
1008 	if (ngrp < 0 || ngrp >= NGROUPS)
1009 		return (EINVAL);
1010 	error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1011 	if (error)
1012 		return (error);
1013 	newcred = crget();
1014 	p = td->td_proc;
1015 	PROC_LOCK(p);
1016 	oldcred = p->p_ucred;
1017 
1018 	/*
1019 	 * cr_groups[0] holds egid. Setting the whole set from
1020 	 * the supplied set will cause egid to be changed too.
1021 	 * Keep cr_groups[0] unchanged to prevent that.
1022 	 */
1023 
1024 	if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS,
1025 	    SUSER_ALLOWJAIL)) != 0) {
1026 		PROC_UNLOCK(p);
1027 		crfree(newcred);
1028 		return (error);
1029 	}
1030 
1031 	crcopy(newcred, oldcred);
1032 	if (ngrp > 0) {
1033 		newcred->cr_ngroups = ngrp + 1;
1034 
1035 		bsd_gidset = newcred->cr_groups;
1036 		ngrp--;
1037 		while (ngrp >= 0) {
1038 			bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1039 			ngrp--;
1040 		}
1041 	}
1042 	else
1043 		newcred->cr_ngroups = 1;
1044 
1045 	setsugid(p);
1046 	p->p_ucred = newcred;
1047 	PROC_UNLOCK(p);
1048 	crfree(oldcred);
1049 	return (0);
1050 }
1051 
1052 int
1053 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1054 {
1055 	struct ucred *cred;
1056 	l_gid_t linux_gidset[NGROUPS];
1057 	gid_t *bsd_gidset;
1058 	int bsd_gidsetsz, ngrp, error;
1059 
1060 	cred = td->td_ucred;
1061 	bsd_gidset = cred->cr_groups;
1062 	bsd_gidsetsz = cred->cr_ngroups - 1;
1063 
1064 	/*
1065 	 * cr_groups[0] holds egid. Returning the whole set
1066 	 * here will cause a duplicate. Exclude cr_groups[0]
1067 	 * to prevent that.
1068 	 */
1069 
1070 	if ((ngrp = args->gidsetsize) == 0) {
1071 		td->td_retval[0] = bsd_gidsetsz;
1072 		return (0);
1073 	}
1074 
1075 	if (ngrp < bsd_gidsetsz)
1076 		return (EINVAL);
1077 
1078 	ngrp = 0;
1079 	while (ngrp < bsd_gidsetsz) {
1080 		linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1081 		ngrp++;
1082 	}
1083 
1084 	if ((error = copyout(linux_gidset, args->grouplist,
1085 	    ngrp * sizeof(l_gid_t))))
1086 		return (error);
1087 
1088 	td->td_retval[0] = ngrp;
1089 	return (0);
1090 }
1091 
1092 int
1093 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1094 {
1095 	struct rlimit bsd_rlim;
1096 	struct l_rlimit rlim;
1097 	u_int which;
1098 	int error;
1099 
1100 #ifdef DEBUG
1101 	if (ldebug(setrlimit))
1102 		printf(ARGS(setrlimit, "%d, %p"),
1103 		    args->resource, (void *)args->rlim);
1104 #endif
1105 
1106 	if (args->resource >= LINUX_RLIM_NLIMITS)
1107 		return (EINVAL);
1108 
1109 	which = linux_to_bsd_resource[args->resource];
1110 	if (which == -1)
1111 		return (EINVAL);
1112 
1113 	error = copyin(args->rlim, &rlim, sizeof(rlim));
1114 	if (error)
1115 		return (error);
1116 
1117 	bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1118 	bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1119 	return (kern_setrlimit(td, which, &bsd_rlim));
1120 }
1121 
1122 int
1123 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1124 {
1125 	struct l_rlimit rlim;
1126 	struct proc *p = td->td_proc;
1127 	struct rlimit bsd_rlim;
1128 	u_int which;
1129 
1130 #ifdef DEBUG
1131 	if (ldebug(old_getrlimit))
1132 		printf(ARGS(old_getrlimit, "%d, %p"),
1133 		    args->resource, (void *)args->rlim);
1134 #endif
1135 
1136 	if (args->resource >= LINUX_RLIM_NLIMITS)
1137 		return (EINVAL);
1138 
1139 	which = linux_to_bsd_resource[args->resource];
1140 	if (which == -1)
1141 		return (EINVAL);
1142 
1143 	PROC_LOCK(p);
1144 	lim_rlimit(p, which, &bsd_rlim);
1145 	PROC_UNLOCK(p);
1146 
1147 #ifdef COMPAT_LINUX32
1148 	rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1149 	if (rlim.rlim_cur == UINT_MAX)
1150 		rlim.rlim_cur = INT_MAX;
1151 	rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1152 	if (rlim.rlim_max == UINT_MAX)
1153 		rlim.rlim_max = INT_MAX;
1154 #else
1155 	rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1156 	if (rlim.rlim_cur == ULONG_MAX)
1157 		rlim.rlim_cur = LONG_MAX;
1158 	rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1159 	if (rlim.rlim_max == ULONG_MAX)
1160 		rlim.rlim_max = LONG_MAX;
1161 #endif
1162 	return (copyout(&rlim, args->rlim, sizeof(rlim)));
1163 }
1164 
1165 int
1166 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1167 {
1168 	struct l_rlimit rlim;
1169 	struct proc *p = td->td_proc;
1170 	struct rlimit bsd_rlim;
1171 	u_int which;
1172 
1173 #ifdef DEBUG
1174 	if (ldebug(getrlimit))
1175 		printf(ARGS(getrlimit, "%d, %p"),
1176 		    args->resource, (void *)args->rlim);
1177 #endif
1178 
1179 	if (args->resource >= LINUX_RLIM_NLIMITS)
1180 		return (EINVAL);
1181 
1182 	which = linux_to_bsd_resource[args->resource];
1183 	if (which == -1)
1184 		return (EINVAL);
1185 
1186 	PROC_LOCK(p);
1187 	lim_rlimit(p, which, &bsd_rlim);
1188 	PROC_UNLOCK(p);
1189 
1190 	rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1191 	rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1192 	return (copyout(&rlim, args->rlim, sizeof(rlim)));
1193 }
1194 
1195 int
1196 linux_sched_setscheduler(struct thread *td,
1197     struct linux_sched_setscheduler_args *args)
1198 {
1199 	struct sched_setscheduler_args bsd;
1200 
1201 #ifdef DEBUG
1202 	if (ldebug(sched_setscheduler))
1203 		printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1204 		    args->pid, args->policy, (const void *)args->param);
1205 #endif
1206 
1207 	switch (args->policy) {
1208 	case LINUX_SCHED_OTHER:
1209 		bsd.policy = SCHED_OTHER;
1210 		break;
1211 	case LINUX_SCHED_FIFO:
1212 		bsd.policy = SCHED_FIFO;
1213 		break;
1214 	case LINUX_SCHED_RR:
1215 		bsd.policy = SCHED_RR;
1216 		break;
1217 	default:
1218 		return EINVAL;
1219 	}
1220 
1221 	bsd.pid = args->pid;
1222 	bsd.param = (struct sched_param *)args->param;
1223 	return sched_setscheduler(td, &bsd);
1224 }
1225 
1226 int
1227 linux_sched_getscheduler(struct thread *td,
1228     struct linux_sched_getscheduler_args *args)
1229 {
1230 	struct sched_getscheduler_args bsd;
1231 	int error;
1232 
1233 #ifdef DEBUG
1234 	if (ldebug(sched_getscheduler))
1235 		printf(ARGS(sched_getscheduler, "%d"), args->pid);
1236 #endif
1237 
1238 	bsd.pid = args->pid;
1239 	error = sched_getscheduler(td, &bsd);
1240 
1241 	switch (td->td_retval[0]) {
1242 	case SCHED_OTHER:
1243 		td->td_retval[0] = LINUX_SCHED_OTHER;
1244 		break;
1245 	case SCHED_FIFO:
1246 		td->td_retval[0] = LINUX_SCHED_FIFO;
1247 		break;
1248 	case SCHED_RR:
1249 		td->td_retval[0] = LINUX_SCHED_RR;
1250 		break;
1251 	}
1252 
1253 	return error;
1254 }
1255 
1256 int
1257 linux_sched_get_priority_max(struct thread *td,
1258     struct linux_sched_get_priority_max_args *args)
1259 {
1260 	struct sched_get_priority_max_args bsd;
1261 
1262 #ifdef DEBUG
1263 	if (ldebug(sched_get_priority_max))
1264 		printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1265 #endif
1266 
1267 	switch (args->policy) {
1268 	case LINUX_SCHED_OTHER:
1269 		bsd.policy = SCHED_OTHER;
1270 		break;
1271 	case LINUX_SCHED_FIFO:
1272 		bsd.policy = SCHED_FIFO;
1273 		break;
1274 	case LINUX_SCHED_RR:
1275 		bsd.policy = SCHED_RR;
1276 		break;
1277 	default:
1278 		return EINVAL;
1279 	}
1280 	return sched_get_priority_max(td, &bsd);
1281 }
1282 
1283 int
1284 linux_sched_get_priority_min(struct thread *td,
1285     struct linux_sched_get_priority_min_args *args)
1286 {
1287 	struct sched_get_priority_min_args bsd;
1288 
1289 #ifdef DEBUG
1290 	if (ldebug(sched_get_priority_min))
1291 		printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1292 #endif
1293 
1294 	switch (args->policy) {
1295 	case LINUX_SCHED_OTHER:
1296 		bsd.policy = SCHED_OTHER;
1297 		break;
1298 	case LINUX_SCHED_FIFO:
1299 		bsd.policy = SCHED_FIFO;
1300 		break;
1301 	case LINUX_SCHED_RR:
1302 		bsd.policy = SCHED_RR;
1303 		break;
1304 	default:
1305 		return EINVAL;
1306 	}
1307 	return sched_get_priority_min(td, &bsd);
1308 }
1309 
1310 #define REBOOT_CAD_ON	0x89abcdef
1311 #define REBOOT_CAD_OFF	0
1312 #define REBOOT_HALT	0xcdef0123
1313 #define REBOOT_RESTART	0x01234567
1314 #define REBOOT_RESTART2	0xA1B2C3D4
1315 #define REBOOT_POWEROFF	0x4321FEDC
1316 #define REBOOT_MAGIC1	0xfee1dead
1317 #define REBOOT_MAGIC2	0x28121969
1318 #define REBOOT_MAGIC2A	0x05121996
1319 #define REBOOT_MAGIC2B	0x16041998
1320 
1321 int
1322 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1323 {
1324 	struct reboot_args bsd_args;
1325 
1326 #ifdef DEBUG
1327 	if (ldebug(reboot))
1328 		printf(ARGS(reboot, "0x%x"), args->cmd);
1329 #endif
1330 
1331 	if (args->magic1 != REBOOT_MAGIC1)
1332 		return EINVAL;
1333 
1334 	switch (args->magic2) {
1335 	case REBOOT_MAGIC2:
1336 	case REBOOT_MAGIC2A:
1337 	case REBOOT_MAGIC2B:
1338 		break;
1339 	default:
1340 		return EINVAL;
1341 	}
1342 
1343 	switch (args->cmd) {
1344 	case REBOOT_CAD_ON:
1345 	case REBOOT_CAD_OFF:
1346 		return (priv_check(td, PRIV_REBOOT));
1347 	case REBOOT_HALT:
1348 		bsd_args.opt = RB_HALT;
1349 		break;
1350 	case REBOOT_RESTART:
1351 	case REBOOT_RESTART2:
1352 		bsd_args.opt = 0;
1353 		break;
1354 	case REBOOT_POWEROFF:
1355 		bsd_args.opt = RB_POWEROFF;
1356 		break;
1357 	default:
1358 		return EINVAL;
1359 	}
1360 	return reboot(td, &bsd_args);
1361 }
1362 
1363 
1364 /*
1365  * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1366  * td->td_retval[1] when COMPAT_43 is defined. This
1367  * globbers registers that are assumed to be preserved. The following
1368  * lightweight syscalls fixes this. See also linux_getgid16() and
1369  * linux_getuid16() in linux_uid16.c.
1370  *
1371  * linux_getpid() - MP SAFE
1372  * linux_getgid() - MP SAFE
1373  * linux_getuid() - MP SAFE
1374  */
1375 
1376 int
1377 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1378 {
1379    	struct linux_emuldata *em;
1380 	char osrel[LINUX_MAX_UTSNAME];
1381 
1382 	linux_get_osrelease(td, osrel);
1383 	if (strlen(osrel) >= 3 && osrel[2] == '6') {
1384    	   	em = em_find(td->td_proc, EMUL_UNLOCKED);
1385 		KASSERT(em != NULL, ("getpid: emuldata not found.\n"));
1386    		td->td_retval[0] = em->shared->group_pid;
1387 		EMUL_UNLOCK(&emul_lock);
1388 	} else {
1389 	   	PROC_LOCK(td->td_proc);
1390    	   	td->td_retval[0] = td->td_proc->p_pid;
1391 	   	PROC_UNLOCK(td->td_proc);
1392 	}
1393 
1394 	return (0);
1395 }
1396 
1397 int
1398 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1399 {
1400 #ifdef DEBUG
1401 	if (ldebug(gettid))
1402 		printf(ARGS(gettid, ""));
1403 #endif
1404 
1405 	td->td_retval[0] = td->td_proc->p_pid;
1406 	return (0);
1407 }
1408 
1409 
1410 int
1411 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1412 {
1413    	struct linux_emuldata *em;
1414 	struct proc *p, *pp;
1415 	char osrel[LINUX_MAX_UTSNAME];
1416 
1417 	linux_get_osrelease(td, osrel);
1418 	if (strlen(osrel) >= 3 && osrel[2] != '6') {
1419 	   	PROC_LOCK(td->td_proc);
1420 	   	td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1421 	   	PROC_UNLOCK(td->td_proc);
1422 		return (0);
1423 	}
1424 
1425 	em = em_find(td->td_proc, EMUL_UNLOCKED);
1426 
1427 	KASSERT(em != NULL, ("getppid: process emuldata not found.\n"));
1428 
1429 	/* find the group leader */
1430 	p = pfind(em->shared->group_pid);
1431 
1432 	if (p == NULL) {
1433 #ifdef DEBUG
1434 	   	printf(LMSG("parent process not found.\n"));
1435 #endif
1436 		return (0);
1437 	}
1438 
1439 	pp = p->p_pptr;		/* switch to parent */
1440 	PROC_LOCK(pp);
1441 	PROC_UNLOCK(p);
1442 
1443 	/* if its also linux process */
1444 	if (pp->p_sysent == &elf_linux_sysvec) {
1445    	   	em = em_find(pp, EMUL_LOCKED);
1446 		KASSERT(em != NULL, ("getppid: parent emuldata not found.\n"));
1447 
1448 		td->td_retval[0] = em->shared->group_pid;
1449 	} else
1450 	   	td->td_retval[0] = pp->p_pid;
1451 
1452 	EMUL_UNLOCK(&emul_lock);
1453 	PROC_UNLOCK(pp);
1454 
1455 	return (0);
1456 }
1457 
1458 int
1459 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1460 {
1461 
1462 	td->td_retval[0] = td->td_ucred->cr_rgid;
1463 	return (0);
1464 }
1465 
1466 int
1467 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1468 {
1469 
1470 	td->td_retval[0] = td->td_ucred->cr_ruid;
1471 	return (0);
1472 }
1473 
1474 
1475 int
1476 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1477 {
1478 	struct getsid_args bsd;
1479 	bsd.pid = args->pid;
1480 	return getsid(td, &bsd);
1481 }
1482 
1483 int
1484 linux_nosys(struct thread *td, struct nosys_args *ignore)
1485 {
1486 
1487 	return (ENOSYS);
1488 }
1489 
1490 int
1491 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1492 {
1493 	struct getpriority_args	bsd_args;
1494 	int error;
1495 
1496 	bsd_args.which = args->which;
1497 	bsd_args.who = args->who;
1498 	error = getpriority(td, &bsd_args);
1499 	td->td_retval[0] = 20 - td->td_retval[0];
1500 	return error;
1501 }
1502 
1503 int
1504 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1505 {
1506 	int name[2];
1507 
1508 	name[0] = CTL_KERN;
1509 	name[1] = KERN_HOSTNAME;
1510 	return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1511 		 args->len, 0, 0));
1512 }
1513 
1514 int
1515 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1516 {
1517    	struct linux_emuldata *em, *td_em, *tmp_em;
1518 	struct proc *sp;
1519 	char osrel[LINUX_MAX_UTSNAME];
1520 
1521 #ifdef DEBUG
1522 	if (ldebug(exit_group))
1523 		printf(ARGS(exit_group, "%i"), args->error_code);
1524 #endif
1525 
1526 	linux_get_osrelease(td, osrel);
1527 	if (strlen(osrel) >= 3 && osrel[2] == '6') {
1528    	   	td_em = em_find(td->td_proc, EMUL_UNLOCKED);
1529 
1530 		KASSERT(td_em != NULL, ("exit_group: emuldata not found.\n"));
1531 
1532    		EMUL_SHARED_RLOCK(&emul_shared_lock);
1533      		LIST_FOREACH_SAFE(em, &td_em->shared->threads, threads, tmp_em) {
1534 	   		if (em->pid == td_em->pid)
1535 		   		continue;
1536 
1537 			sp = pfind(em->pid);
1538 			psignal(sp, SIGKILL);
1539 			PROC_UNLOCK(sp);
1540 #ifdef DEBUG
1541 			printf(LMSG("linux_sys_exit_group: kill PID %d\n"), em->pid);
1542 #endif
1543 		}
1544 
1545 		EMUL_SHARED_RUNLOCK(&emul_shared_lock);
1546 		EMUL_UNLOCK(&emul_lock);
1547 	}
1548 
1549 	exit1(td, W_EXITCODE(args->error_code,0));
1550 
1551    	return (0);
1552 }
1553 
1554 int
1555 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1556 {
1557    	int error = 0;
1558 	struct proc *p = td->td_proc;
1559 	char comm[LINUX_MAX_COMM_LEN];
1560 	struct linux_emuldata *em;
1561 
1562 #ifdef DEBUG
1563 	if (ldebug(prctl))
1564 	   	printf(ARGS(prctl, "%d, %d, %d, %d, %d"), args->option, args->arg2,
1565 		      args->arg3, args->arg4, args->arg5);
1566 #endif
1567 
1568    	switch (args->option) {
1569    	case LINUX_PR_SET_PDEATHSIG:
1570 	   	if (!LINUX_SIG_VALID(args->arg2))
1571 		   	return (EINVAL);
1572 		em = em_find(p, EMUL_UNLOCKED);
1573 		KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1574 		em->pdeath_signal = args->arg2;
1575 		EMUL_UNLOCK(&emul_lock);
1576 	   	break;
1577 	case LINUX_PR_GET_PDEATHSIG:
1578 		em = em_find(p, EMUL_UNLOCKED);
1579 		KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1580 		error = copyout(&em->pdeath_signal, (void *)(register_t) args->arg2, sizeof(em->pdeath_signal));
1581 		EMUL_UNLOCK(&emul_lock);
1582 		break;
1583 	case LINUX_PR_SET_NAME:
1584 		comm[LINUX_MAX_COMM_LEN-1] = 0;
1585 		error = copyin(comm, (void *)(register_t) args->arg2, LINUX_MAX_COMM_LEN-1);
1586 		if (error)
1587 		   	return (error);
1588 		PROC_LOCK(p);
1589 		strcpy(p->p_comm, comm);
1590 		PROC_UNLOCK(p);
1591 		break;
1592 	case LINUX_PR_GET_NAME:
1593 		error = copyout(&p->p_comm, (void *)(register_t) args->arg2, MAXCOMLEN+1);
1594 		break;
1595 	default:
1596 		error = EINVAL;
1597 		break;
1598 	}
1599 
1600 	return (error);
1601 }
1602