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