xref: /freebsd/sys/compat/linux/linux_misc.c (revision 0c927cdd8e6e05387fc5a9ffcb5dbe128d4ad749)
1 /*-
2  * Copyright (c) 2002 Doug Rabson
3  * Copyright (c) 1994-1995 S�ren Schmidt
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer
11  *    in this position and unchanged.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include "opt_compat.h"
34 #include "opt_mac.h"
35 
36 #include <sys/param.h>
37 #include <sys/blist.h>
38 #include <sys/fcntl.h>
39 #if defined(__i386__)
40 #include <sys/imgact_aout.h>
41 #endif
42 #include <sys/jail.h>
43 #include <sys/kernel.h>
44 #include <sys/limits.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mman.h>
48 #include <sys/mount.h>
49 #include <sys/mutex.h>
50 #include <sys/namei.h>
51 #include <sys/priv.h>
52 #include <sys/proc.h>
53 #include <sys/reboot.h>
54 #include <sys/resourcevar.h>
55 #include <sys/sched.h>
56 #include <sys/signalvar.h>
57 #include <sys/stat.h>
58 #include <sys/syscallsubr.h>
59 #include <sys/sysctl.h>
60 #include <sys/sysproto.h>
61 #include <sys/systm.h>
62 #include <sys/time.h>
63 #include <sys/vmmeter.h>
64 #include <sys/vnode.h>
65 #include <sys/wait.h>
66 
67 #include <security/mac/mac_framework.h>
68 
69 #include <vm/vm.h>
70 #include <vm/pmap.h>
71 #include <vm/vm_kern.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_extern.h>
74 #include <vm/vm_object.h>
75 #include <vm/swap_pager.h>
76 
77 #include <compat/linux/linux_sysproto.h>
78 #include <compat/linux/linux_emul.h>
79 #include <compat/linux/linux_misc.h>
80 
81 #ifdef COMPAT_LINUX32
82 #include <machine/../linux32/linux.h>
83 #include <machine/../linux32/linux32_proto.h>
84 #else
85 #include <machine/../linux/linux.h>
86 #include <machine/../linux/linux_proto.h>
87 #endif
88 
89 #include <compat/linux/linux_mib.h>
90 #include <compat/linux/linux_signal.h>
91 #include <compat/linux/linux_util.h>
92 
93 #ifdef __i386__
94 #include <machine/cputypes.h>
95 #endif
96 
97 #define BSD_TO_LINUX_SIGNAL(sig)	\
98 	(((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
99 
100 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
101 	RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
102 	RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
103 	RLIMIT_MEMLOCK, RLIMIT_AS
104 };
105 
106 struct l_sysinfo {
107 	l_long		uptime;		/* Seconds since boot */
108 	l_ulong		loads[3];	/* 1, 5, and 15 minute load averages */
109 #define LINUX_SYSINFO_LOADS_SCALE 65536
110 	l_ulong		totalram;	/* Total usable main memory size */
111 	l_ulong		freeram;	/* Available memory size */
112 	l_ulong		sharedram;	/* Amount of shared memory */
113 	l_ulong		bufferram;	/* Memory used by buffers */
114 	l_ulong		totalswap;	/* Total swap space size */
115 	l_ulong		freeswap;	/* swap space still available */
116 	l_ushort	procs;		/* Number of current processes */
117 	l_ushort	pads;
118 	l_ulong		totalbig;
119 	l_ulong		freebig;
120 	l_uint		mem_unit;
121 	char		_f[20-2*sizeof(l_long)-sizeof(l_int)];	/* padding */
122 };
123 int
124 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
125 {
126 	struct l_sysinfo sysinfo;
127 	vm_object_t object;
128 	int i, j;
129 	struct timespec ts;
130 
131 	getnanouptime(&ts);
132 	if (ts.tv_nsec != 0)
133 		ts.tv_sec++;
134 	sysinfo.uptime = ts.tv_sec;
135 
136 	/* Use the information from the mib to get our load averages */
137 	for (i = 0; i < 3; i++)
138 		sysinfo.loads[i] = averunnable.ldavg[i] *
139 		    LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
140 
141 	sysinfo.totalram = physmem * PAGE_SIZE;
142 	sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
143 
144 	sysinfo.sharedram = 0;
145 	mtx_lock(&vm_object_list_mtx);
146 	TAILQ_FOREACH(object, &vm_object_list, object_list)
147 		if (object->shadow_count > 1)
148 			sysinfo.sharedram += object->resident_page_count;
149 	mtx_unlock(&vm_object_list_mtx);
150 
151 	sysinfo.sharedram *= PAGE_SIZE;
152 	sysinfo.bufferram = 0;
153 
154 	swap_pager_status(&i, &j);
155 	sysinfo.totalswap = i * PAGE_SIZE;
156 	sysinfo.freeswap = (i - j) * PAGE_SIZE;
157 
158 	sysinfo.procs = nprocs;
159 
160 	/* The following are only present in newer Linux kernels. */
161 	sysinfo.totalbig = 0;
162 	sysinfo.freebig = 0;
163 	sysinfo.mem_unit = 1;
164 
165 	return copyout(&sysinfo, args->info, sizeof(sysinfo));
166 }
167 
168 int
169 linux_alarm(struct thread *td, struct linux_alarm_args *args)
170 {
171 	struct itimerval it, old_it;
172 	int error;
173 
174 #ifdef DEBUG
175 	if (ldebug(alarm))
176 		printf(ARGS(alarm, "%u"), args->secs);
177 #endif
178 
179 	if (args->secs > 100000000)
180 		return (EINVAL);
181 
182 	it.it_value.tv_sec = (long)args->secs;
183 	it.it_value.tv_usec = 0;
184 	it.it_interval.tv_sec = 0;
185 	it.it_interval.tv_usec = 0;
186 	error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
187 	if (error)
188 		return (error);
189 	if (timevalisset(&old_it.it_value)) {
190 		if (old_it.it_value.tv_usec != 0)
191 			old_it.it_value.tv_sec++;
192 		td->td_retval[0] = old_it.it_value.tv_sec;
193 	}
194 	return (0);
195 }
196 
197 int
198 linux_brk(struct thread *td, struct linux_brk_args *args)
199 {
200 	struct vmspace *vm = td->td_proc->p_vmspace;
201 	vm_offset_t new, old;
202 	struct obreak_args /* {
203 		char * nsize;
204 	} */ tmp;
205 
206 #ifdef DEBUG
207 	if (ldebug(brk))
208 		printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
209 #endif
210 	old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
211 	new = (vm_offset_t)args->dsend;
212 	tmp.nsize = (char *)new;
213 	if (((caddr_t)new > vm->vm_daddr) && !obreak(td, &tmp))
214 		td->td_retval[0] = (long)new;
215 	else
216 		td->td_retval[0] = (long)old;
217 
218 	return 0;
219 }
220 
221 #if defined(__i386__)
222 /* XXX: what about amd64/linux32? */
223 
224 int
225 linux_uselib(struct thread *td, struct linux_uselib_args *args)
226 {
227 	struct nameidata ni;
228 	struct vnode *vp;
229 	struct exec *a_out;
230 	struct vattr attr;
231 	vm_offset_t vmaddr;
232 	unsigned long file_offset;
233 	vm_offset_t buffer;
234 	unsigned long bss_size;
235 	char *library;
236 	int error;
237 	int locked, vfslocked;
238 
239 	LCONVPATHEXIST(td, args->library, &library);
240 
241 #ifdef DEBUG
242 	if (ldebug(uselib))
243 		printf(ARGS(uselib, "%s"), library);
244 #endif
245 
246 	a_out = NULL;
247 	vfslocked = 0;
248 	locked = 0;
249 	vp = NULL;
250 
251 	NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
252 	    UIO_SYSSPACE, library, td);
253 	error = namei(&ni);
254 	LFREEPATH(library);
255 	if (error)
256 		goto cleanup;
257 
258 	vp = ni.ni_vp;
259 	vfslocked = NDHASGIANT(&ni);
260 	NDFREE(&ni, NDF_ONLY_PNBUF);
261 
262 	/*
263 	 * From here on down, we have a locked vnode that must be unlocked.
264 	 * XXX: The code below largely duplicates exec_check_permissions().
265 	 */
266 	locked = 1;
267 
268 	/* Writable? */
269 	if (vp->v_writecount) {
270 		error = ETXTBSY;
271 		goto cleanup;
272 	}
273 
274 	/* Executable? */
275 	error = VOP_GETATTR(vp, &attr, td->td_ucred, td);
276 	if (error)
277 		goto cleanup;
278 
279 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
280 	    ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
281 		/* EACCESS is what exec(2) returns. */
282 		error = ENOEXEC;
283 		goto cleanup;
284 	}
285 
286 	/* Sensible size? */
287 	if (attr.va_size == 0) {
288 		error = ENOEXEC;
289 		goto cleanup;
290 	}
291 
292 	/* Can we access it? */
293 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
294 	if (error)
295 		goto cleanup;
296 
297 	/*
298 	 * XXX: This should use vn_open() so that it is properly authorized,
299 	 * and to reduce code redundancy all over the place here.
300 	 * XXX: Not really, it duplicates far more of exec_check_permissions()
301 	 * than vn_open().
302 	 */
303 #ifdef MAC
304 	error = mac_check_vnode_open(td->td_ucred, vp, FREAD);
305 	if (error)
306 		goto cleanup;
307 #endif
308 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
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 		PROC_SLOCK(p);
674 		calcru(p, &utime, &stime);
675 		PROC_SUNLOCK(p);
676 		calccru(p, &cutime, &cstime);
677 		PROC_UNLOCK(p);
678 
679 		tms.tms_utime = CONVTCK(utime);
680 		tms.tms_stime = CONVTCK(stime);
681 
682 		tms.tms_cutime = CONVTCK(cutime);
683 		tms.tms_cstime = CONVTCK(cstime);
684 
685 		if ((error = copyout(&tms, args->buf, sizeof(tms))))
686 			return error;
687 	}
688 
689 	microuptime(&tv);
690 	td->td_retval[0] = (int)CONVTCK(tv);
691 	return 0;
692 }
693 
694 int
695 linux_newuname(struct thread *td, struct linux_newuname_args *args)
696 {
697 	struct l_new_utsname utsname;
698 	char osname[LINUX_MAX_UTSNAME];
699 	char osrelease[LINUX_MAX_UTSNAME];
700 	char *p;
701 
702 #ifdef DEBUG
703 	if (ldebug(newuname))
704 		printf(ARGS(newuname, "*"));
705 #endif
706 
707 	linux_get_osname(td, osname);
708 	linux_get_osrelease(td, osrelease);
709 
710 	bzero(&utsname, sizeof(utsname));
711 	strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
712 	getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
713 	strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
714 	strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
715 	for (p = utsname.version; *p != '\0'; ++p)
716 		if (*p == '\n') {
717 			*p = '\0';
718 			break;
719 		}
720 #ifdef __i386__
721 	{
722 		const char *class;
723 
724 		switch (cpu_class) {
725 		case CPUCLASS_686:
726 			class = "i686";
727 			break;
728 		case CPUCLASS_586:
729 			class = "i586";
730 			break;
731 		case CPUCLASS_486:
732 			class = "i486";
733 			break;
734 		default:
735 			class = "i386";
736 		}
737 		strlcpy(utsname.machine, class, LINUX_MAX_UTSNAME);
738 	}
739 #elif defined(__amd64__)	/* XXX: Linux can change 'personality'. */
740 #ifdef COMPAT_LINUX32
741 	strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME);
742 #else
743 	strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME);
744 #endif /* COMPAT_LINUX32 */
745 #else /* something other than i386 or amd64 - assume we and Linux agree */
746 	strlcpy(utsname.machine, machine, LINUX_MAX_UTSNAME);
747 #endif /* __i386__ */
748 	strlcpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME);
749 
750 	return (copyout(&utsname, args->buf, sizeof(utsname)));
751 }
752 
753 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
754 struct l_utimbuf {
755 	l_time_t l_actime;
756 	l_time_t l_modtime;
757 };
758 
759 int
760 linux_utime(struct thread *td, struct linux_utime_args *args)
761 {
762 	struct timeval tv[2], *tvp;
763 	struct l_utimbuf lut;
764 	char *fname;
765 	int error;
766 
767 	LCONVPATHEXIST(td, args->fname, &fname);
768 
769 #ifdef DEBUG
770 	if (ldebug(utime))
771 		printf(ARGS(utime, "%s, *"), fname);
772 #endif
773 
774 	if (args->times) {
775 		if ((error = copyin(args->times, &lut, sizeof lut))) {
776 			LFREEPATH(fname);
777 			return error;
778 		}
779 		tv[0].tv_sec = lut.l_actime;
780 		tv[0].tv_usec = 0;
781 		tv[1].tv_sec = lut.l_modtime;
782 		tv[1].tv_usec = 0;
783 		tvp = tv;
784 	} else
785 		tvp = NULL;
786 
787 	error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
788 	LFREEPATH(fname);
789 	return (error);
790 }
791 
792 int
793 linux_utimes(struct thread *td, struct linux_utimes_args *args)
794 {
795 	l_timeval ltv[2];
796 	struct timeval tv[2], *tvp = NULL;
797 	char *fname;
798 	int error;
799 
800 	LCONVPATHEXIST(td, args->fname, &fname);
801 
802 #ifdef DEBUG
803 	if (ldebug(utimes))
804 		printf(ARGS(utimes, "%s, *"), fname);
805 #endif
806 
807 	if (args->tptr != NULL) {
808 		if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
809 			LFREEPATH(fname);
810 			return (error);
811 		}
812 		tv[0].tv_sec = ltv[0].tv_sec;
813 		tv[0].tv_usec = ltv[0].tv_usec;
814 		tv[1].tv_sec = ltv[1].tv_sec;
815 		tv[1].tv_usec = ltv[1].tv_usec;
816 		tvp = tv;
817 	}
818 
819 	error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
820 	LFREEPATH(fname);
821 	return (error);
822 }
823 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
824 
825 #define __WCLONE 0x80000000
826 
827 int
828 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
829 {
830 	int error, options, tmpstat;
831 
832 #ifdef DEBUG
833 	if (ldebug(waitpid))
834 		printf(ARGS(waitpid, "%d, %p, %d"),
835 		    args->pid, (void *)args->status, args->options);
836 #endif
837 	/*
838 	 * this is necessary because the test in kern_wait doesn't work
839 	 * because we mess with the options here
840 	 */
841 	if (args->options & ~(WUNTRACED | WNOHANG | WCONTINUED | __WCLONE))
842 		return (EINVAL);
843 
844 	options = (args->options & (WNOHANG | WUNTRACED));
845 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
846 	if (args->options & __WCLONE)
847 		options |= WLINUXCLONE;
848 
849 	error = kern_wait(td, args->pid, &tmpstat, options, NULL);
850 	if (error)
851 		return error;
852 
853 	if (args->status) {
854 		tmpstat &= 0xffff;
855 		if (WIFSIGNALED(tmpstat))
856 			tmpstat = (tmpstat & 0xffffff80) |
857 			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
858 		else if (WIFSTOPPED(tmpstat))
859 			tmpstat = (tmpstat & 0xffff00ff) |
860 			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
861 		return copyout(&tmpstat, args->status, sizeof(int));
862 	}
863 
864 	return 0;
865 }
866 
867 int
868 linux_wait4(struct thread *td, struct linux_wait4_args *args)
869 {
870 	int error, options, tmpstat;
871 	struct rusage ru, *rup;
872 	struct proc *p;
873 
874 #ifdef DEBUG
875 	if (ldebug(wait4))
876 		printf(ARGS(wait4, "%d, %p, %d, %p"),
877 		    args->pid, (void *)args->status, args->options,
878 		    (void *)args->rusage);
879 #endif
880 
881 	options = (args->options & (WNOHANG | WUNTRACED));
882 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
883 	if (args->options & __WCLONE)
884 		options |= WLINUXCLONE;
885 
886 	if (args->rusage != NULL)
887 		rup = &ru;
888 	else
889 		rup = NULL;
890 	error = kern_wait(td, args->pid, &tmpstat, options, rup);
891 	if (error)
892 		return error;
893 
894 	p = td->td_proc;
895 	PROC_LOCK(p);
896 	sigqueue_delete(&p->p_sigqueue, SIGCHLD);
897 	PROC_UNLOCK(p);
898 
899 	if (args->status) {
900 		tmpstat &= 0xffff;
901 		if (WIFSIGNALED(tmpstat))
902 			tmpstat = (tmpstat & 0xffffff80) |
903 			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
904 		else if (WIFSTOPPED(tmpstat))
905 			tmpstat = (tmpstat & 0xffff00ff) |
906 			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
907 		error = copyout(&tmpstat, args->status, sizeof(int));
908 	}
909 	if (args->rusage != NULL && error == 0)
910 		error = copyout(&ru, args->rusage, sizeof(ru));
911 
912 	return (error);
913 }
914 
915 int
916 linux_mknod(struct thread *td, struct linux_mknod_args *args)
917 {
918 	char *path;
919 	int error;
920 
921 	LCONVPATHCREAT(td, args->path, &path);
922 
923 #ifdef DEBUG
924 	if (ldebug(mknod))
925 		printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
926 #endif
927 
928 	switch (args->mode & S_IFMT) {
929 	case S_IFIFO:
930 	case S_IFSOCK:
931 		error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
932 		break;
933 
934 	case S_IFCHR:
935 	case S_IFBLK:
936 		error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
937 		    args->dev);
938 		break;
939 
940 	case S_IFDIR:
941 		error = EPERM;
942 		break;
943 
944 	case 0:
945 		args->mode |= S_IFREG;
946 		/* FALLTHROUGH */
947 	case S_IFREG:
948 		error = kern_open(td, path, UIO_SYSSPACE,
949 		    O_WRONLY | O_CREAT | O_TRUNC, args->mode);
950 		break;
951 
952 	default:
953 		error = EINVAL;
954 		break;
955 	}
956 	LFREEPATH(path);
957 	return (error);
958 }
959 
960 /*
961  * UGH! This is just about the dumbest idea I've ever heard!!
962  */
963 int
964 linux_personality(struct thread *td, struct linux_personality_args *args)
965 {
966 #ifdef DEBUG
967 	if (ldebug(personality))
968 		printf(ARGS(personality, "%lu"), (unsigned long)args->per);
969 #endif
970 	if (args->per != 0)
971 		return EINVAL;
972 
973 	/* Yes Jim, it's still a Linux... */
974 	td->td_retval[0] = 0;
975 	return 0;
976 }
977 
978 struct l_itimerval {
979 	l_timeval it_interval;
980 	l_timeval it_value;
981 };
982 
983 #define	B2L_ITIMERVAL(bip, lip) 					\
984 	(bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec;		\
985 	(bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec;	\
986 	(bip)->it_value.tv_sec = (lip)->it_value.tv_sec;		\
987 	(bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
988 
989 int
990 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
991 {
992 	int error;
993 	struct l_itimerval ls;
994 	struct itimerval aitv, oitv;
995 
996 #ifdef DEBUG
997 	if (ldebug(setitimer))
998 		printf(ARGS(setitimer, "%p, %p"),
999 		    (void *)uap->itv, (void *)uap->oitv);
1000 #endif
1001 
1002 	if (uap->itv == NULL) {
1003 		uap->itv = uap->oitv;
1004 		return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1005 	}
1006 
1007 	error = copyin(uap->itv, &ls, sizeof(ls));
1008 	if (error != 0)
1009 		return (error);
1010 	B2L_ITIMERVAL(&aitv, &ls);
1011 #ifdef DEBUG
1012 	if (ldebug(setitimer)) {
1013 		printf("setitimer: value: sec: %jd, usec: %ld\n",
1014 		    (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1015 		printf("setitimer: interval: sec: %jd, usec: %ld\n",
1016 		    (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1017 	}
1018 #endif
1019 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
1020 	if (error != 0 || uap->oitv == NULL)
1021 		return (error);
1022 	B2L_ITIMERVAL(&ls, &oitv);
1023 
1024 	return (copyout(&ls, uap->oitv, sizeof(ls)));
1025 }
1026 
1027 int
1028 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1029 {
1030 	int error;
1031 	struct l_itimerval ls;
1032 	struct itimerval aitv;
1033 
1034 #ifdef DEBUG
1035 	if (ldebug(getitimer))
1036 		printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1037 #endif
1038 	error = kern_getitimer(td, uap->which, &aitv);
1039 	if (error != 0)
1040 		return (error);
1041 	B2L_ITIMERVAL(&ls, &aitv);
1042 	return (copyout(&ls, uap->itv, sizeof(ls)));
1043 }
1044 
1045 int
1046 linux_nice(struct thread *td, struct linux_nice_args *args)
1047 {
1048 	struct setpriority_args bsd_args;
1049 
1050 	bsd_args.which = PRIO_PROCESS;
1051 	bsd_args.who = 0;		/* current process */
1052 	bsd_args.prio = args->inc;
1053 	return setpriority(td, &bsd_args);
1054 }
1055 
1056 int
1057 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1058 {
1059 	struct ucred *newcred, *oldcred;
1060 	l_gid_t linux_gidset[NGROUPS];
1061 	gid_t *bsd_gidset;
1062 	int ngrp, error;
1063 	struct proc *p;
1064 
1065 	ngrp = args->gidsetsize;
1066 	if (ngrp < 0 || ngrp >= NGROUPS)
1067 		return (EINVAL);
1068 	error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1069 	if (error)
1070 		return (error);
1071 	newcred = crget();
1072 	p = td->td_proc;
1073 	PROC_LOCK(p);
1074 	oldcred = p->p_ucred;
1075 
1076 	/*
1077 	 * cr_groups[0] holds egid. Setting the whole set from
1078 	 * the supplied set will cause egid to be changed too.
1079 	 * Keep cr_groups[0] unchanged to prevent that.
1080 	 */
1081 
1082 	if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS,
1083 	    SUSER_ALLOWJAIL)) != 0) {
1084 		PROC_UNLOCK(p);
1085 		crfree(newcred);
1086 		return (error);
1087 	}
1088 
1089 	crcopy(newcred, oldcred);
1090 	if (ngrp > 0) {
1091 		newcred->cr_ngroups = ngrp + 1;
1092 
1093 		bsd_gidset = newcred->cr_groups;
1094 		ngrp--;
1095 		while (ngrp >= 0) {
1096 			bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1097 			ngrp--;
1098 		}
1099 	} else
1100 		newcred->cr_ngroups = 1;
1101 
1102 	setsugid(p);
1103 	p->p_ucred = newcred;
1104 	PROC_UNLOCK(p);
1105 	crfree(oldcred);
1106 	return (0);
1107 }
1108 
1109 int
1110 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1111 {
1112 	struct ucred *cred;
1113 	l_gid_t linux_gidset[NGROUPS];
1114 	gid_t *bsd_gidset;
1115 	int bsd_gidsetsz, ngrp, error;
1116 
1117 	cred = td->td_ucred;
1118 	bsd_gidset = cred->cr_groups;
1119 	bsd_gidsetsz = cred->cr_ngroups - 1;
1120 
1121 	/*
1122 	 * cr_groups[0] holds egid. Returning the whole set
1123 	 * here will cause a duplicate. Exclude cr_groups[0]
1124 	 * to prevent that.
1125 	 */
1126 
1127 	if ((ngrp = args->gidsetsize) == 0) {
1128 		td->td_retval[0] = bsd_gidsetsz;
1129 		return (0);
1130 	}
1131 
1132 	if (ngrp < bsd_gidsetsz)
1133 		return (EINVAL);
1134 
1135 	ngrp = 0;
1136 	while (ngrp < bsd_gidsetsz) {
1137 		linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1138 		ngrp++;
1139 	}
1140 
1141 	if ((error = copyout(linux_gidset, args->grouplist,
1142 	    ngrp * sizeof(l_gid_t))))
1143 		return (error);
1144 
1145 	td->td_retval[0] = ngrp;
1146 	return (0);
1147 }
1148 
1149 int
1150 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1151 {
1152 	struct rlimit bsd_rlim;
1153 	struct l_rlimit rlim;
1154 	u_int which;
1155 	int error;
1156 
1157 #ifdef DEBUG
1158 	if (ldebug(setrlimit))
1159 		printf(ARGS(setrlimit, "%d, %p"),
1160 		    args->resource, (void *)args->rlim);
1161 #endif
1162 
1163 	if (args->resource >= LINUX_RLIM_NLIMITS)
1164 		return (EINVAL);
1165 
1166 	which = linux_to_bsd_resource[args->resource];
1167 	if (which == -1)
1168 		return (EINVAL);
1169 
1170 	error = copyin(args->rlim, &rlim, sizeof(rlim));
1171 	if (error)
1172 		return (error);
1173 
1174 	bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1175 	bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1176 	return (kern_setrlimit(td, which, &bsd_rlim));
1177 }
1178 
1179 int
1180 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1181 {
1182 	struct l_rlimit rlim;
1183 	struct proc *p = td->td_proc;
1184 	struct rlimit bsd_rlim;
1185 	u_int which;
1186 
1187 #ifdef DEBUG
1188 	if (ldebug(old_getrlimit))
1189 		printf(ARGS(old_getrlimit, "%d, %p"),
1190 		    args->resource, (void *)args->rlim);
1191 #endif
1192 
1193 	if (args->resource >= LINUX_RLIM_NLIMITS)
1194 		return (EINVAL);
1195 
1196 	which = linux_to_bsd_resource[args->resource];
1197 	if (which == -1)
1198 		return (EINVAL);
1199 
1200 	PROC_LOCK(p);
1201 	lim_rlimit(p, which, &bsd_rlim);
1202 	PROC_UNLOCK(p);
1203 
1204 #ifdef COMPAT_LINUX32
1205 	rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1206 	if (rlim.rlim_cur == UINT_MAX)
1207 		rlim.rlim_cur = INT_MAX;
1208 	rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1209 	if (rlim.rlim_max == UINT_MAX)
1210 		rlim.rlim_max = INT_MAX;
1211 #else
1212 	rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1213 	if (rlim.rlim_cur == ULONG_MAX)
1214 		rlim.rlim_cur = LONG_MAX;
1215 	rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1216 	if (rlim.rlim_max == ULONG_MAX)
1217 		rlim.rlim_max = LONG_MAX;
1218 #endif
1219 	return (copyout(&rlim, args->rlim, sizeof(rlim)));
1220 }
1221 
1222 int
1223 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1224 {
1225 	struct l_rlimit rlim;
1226 	struct proc *p = td->td_proc;
1227 	struct rlimit bsd_rlim;
1228 	u_int which;
1229 
1230 #ifdef DEBUG
1231 	if (ldebug(getrlimit))
1232 		printf(ARGS(getrlimit, "%d, %p"),
1233 		    args->resource, (void *)args->rlim);
1234 #endif
1235 
1236 	if (args->resource >= LINUX_RLIM_NLIMITS)
1237 		return (EINVAL);
1238 
1239 	which = linux_to_bsd_resource[args->resource];
1240 	if (which == -1)
1241 		return (EINVAL);
1242 
1243 	PROC_LOCK(p);
1244 	lim_rlimit(p, which, &bsd_rlim);
1245 	PROC_UNLOCK(p);
1246 
1247 	rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1248 	rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1249 	return (copyout(&rlim, args->rlim, sizeof(rlim)));
1250 }
1251 
1252 int
1253 linux_sched_setscheduler(struct thread *td,
1254     struct linux_sched_setscheduler_args *args)
1255 {
1256 	struct sched_setscheduler_args bsd;
1257 
1258 #ifdef DEBUG
1259 	if (ldebug(sched_setscheduler))
1260 		printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1261 		    args->pid, args->policy, (const void *)args->param);
1262 #endif
1263 
1264 	switch (args->policy) {
1265 	case LINUX_SCHED_OTHER:
1266 		bsd.policy = SCHED_OTHER;
1267 		break;
1268 	case LINUX_SCHED_FIFO:
1269 		bsd.policy = SCHED_FIFO;
1270 		break;
1271 	case LINUX_SCHED_RR:
1272 		bsd.policy = SCHED_RR;
1273 		break;
1274 	default:
1275 		return EINVAL;
1276 	}
1277 
1278 	bsd.pid = args->pid;
1279 	bsd.param = (struct sched_param *)args->param;
1280 	return sched_setscheduler(td, &bsd);
1281 }
1282 
1283 int
1284 linux_sched_getscheduler(struct thread *td,
1285     struct linux_sched_getscheduler_args *args)
1286 {
1287 	struct sched_getscheduler_args bsd;
1288 	int error;
1289 
1290 #ifdef DEBUG
1291 	if (ldebug(sched_getscheduler))
1292 		printf(ARGS(sched_getscheduler, "%d"), args->pid);
1293 #endif
1294 
1295 	bsd.pid = args->pid;
1296 	error = sched_getscheduler(td, &bsd);
1297 
1298 	switch (td->td_retval[0]) {
1299 	case SCHED_OTHER:
1300 		td->td_retval[0] = LINUX_SCHED_OTHER;
1301 		break;
1302 	case SCHED_FIFO:
1303 		td->td_retval[0] = LINUX_SCHED_FIFO;
1304 		break;
1305 	case SCHED_RR:
1306 		td->td_retval[0] = LINUX_SCHED_RR;
1307 		break;
1308 	}
1309 
1310 	return error;
1311 }
1312 
1313 int
1314 linux_sched_get_priority_max(struct thread *td,
1315     struct linux_sched_get_priority_max_args *args)
1316 {
1317 	struct sched_get_priority_max_args bsd;
1318 
1319 #ifdef DEBUG
1320 	if (ldebug(sched_get_priority_max))
1321 		printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1322 #endif
1323 
1324 	switch (args->policy) {
1325 	case LINUX_SCHED_OTHER:
1326 		bsd.policy = SCHED_OTHER;
1327 		break;
1328 	case LINUX_SCHED_FIFO:
1329 		bsd.policy = SCHED_FIFO;
1330 		break;
1331 	case LINUX_SCHED_RR:
1332 		bsd.policy = SCHED_RR;
1333 		break;
1334 	default:
1335 		return EINVAL;
1336 	}
1337 	return sched_get_priority_max(td, &bsd);
1338 }
1339 
1340 int
1341 linux_sched_get_priority_min(struct thread *td,
1342     struct linux_sched_get_priority_min_args *args)
1343 {
1344 	struct sched_get_priority_min_args bsd;
1345 
1346 #ifdef DEBUG
1347 	if (ldebug(sched_get_priority_min))
1348 		printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1349 #endif
1350 
1351 	switch (args->policy) {
1352 	case LINUX_SCHED_OTHER:
1353 		bsd.policy = SCHED_OTHER;
1354 		break;
1355 	case LINUX_SCHED_FIFO:
1356 		bsd.policy = SCHED_FIFO;
1357 		break;
1358 	case LINUX_SCHED_RR:
1359 		bsd.policy = SCHED_RR;
1360 		break;
1361 	default:
1362 		return EINVAL;
1363 	}
1364 	return sched_get_priority_min(td, &bsd);
1365 }
1366 
1367 #define REBOOT_CAD_ON	0x89abcdef
1368 #define REBOOT_CAD_OFF	0
1369 #define REBOOT_HALT	0xcdef0123
1370 #define REBOOT_RESTART	0x01234567
1371 #define REBOOT_RESTART2	0xA1B2C3D4
1372 #define REBOOT_POWEROFF	0x4321FEDC
1373 #define REBOOT_MAGIC1	0xfee1dead
1374 #define REBOOT_MAGIC2	0x28121969
1375 #define REBOOT_MAGIC2A	0x05121996
1376 #define REBOOT_MAGIC2B	0x16041998
1377 
1378 int
1379 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1380 {
1381 	struct reboot_args bsd_args;
1382 
1383 #ifdef DEBUG
1384 	if (ldebug(reboot))
1385 		printf(ARGS(reboot, "0x%x"), args->cmd);
1386 #endif
1387 
1388 	if (args->magic1 != REBOOT_MAGIC1)
1389 		return EINVAL;
1390 
1391 	switch (args->magic2) {
1392 	case REBOOT_MAGIC2:
1393 	case REBOOT_MAGIC2A:
1394 	case REBOOT_MAGIC2B:
1395 		break;
1396 	default:
1397 		return EINVAL;
1398 	}
1399 
1400 	switch (args->cmd) {
1401 	case REBOOT_CAD_ON:
1402 	case REBOOT_CAD_OFF:
1403 		return (priv_check(td, PRIV_REBOOT));
1404 	case REBOOT_HALT:
1405 		bsd_args.opt = RB_HALT;
1406 		break;
1407 	case REBOOT_RESTART:
1408 	case REBOOT_RESTART2:
1409 		bsd_args.opt = 0;
1410 		break;
1411 	case REBOOT_POWEROFF:
1412 		bsd_args.opt = RB_POWEROFF;
1413 		break;
1414 	default:
1415 		return EINVAL;
1416 	}
1417 	return reboot(td, &bsd_args);
1418 }
1419 
1420 
1421 /*
1422  * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1423  * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1424  * are assumed to be preserved. The following lightweight syscalls fixes
1425  * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1426  *
1427  * linux_getpid() - MP SAFE
1428  * linux_getgid() - MP SAFE
1429  * linux_getuid() - MP SAFE
1430  */
1431 
1432 int
1433 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1434 {
1435 	struct linux_emuldata *em;
1436 
1437 #ifdef DEBUG
1438 	if (ldebug(getpid))
1439 		printf(ARGS(getpid, ""));
1440 #endif
1441 
1442 	if (linux_use26(td)) {
1443 		em = em_find(td->td_proc, EMUL_DONTLOCK);
1444 		KASSERT(em != NULL, ("getpid: emuldata not found.\n"));
1445 		td->td_retval[0] = em->shared->group_pid;
1446 	} else {
1447 		td->td_retval[0] = td->td_proc->p_pid;
1448 	}
1449 
1450 	return (0);
1451 }
1452 
1453 int
1454 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1455 {
1456 
1457 #ifdef DEBUG
1458 	if (ldebug(gettid))
1459 		printf(ARGS(gettid, ""));
1460 #endif
1461 
1462 	td->td_retval[0] = td->td_proc->p_pid;
1463 	return (0);
1464 }
1465 
1466 
1467 int
1468 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1469 {
1470 	struct linux_emuldata *em;
1471 	struct proc *p, *pp;
1472 
1473 #ifdef DEBUG
1474 	if (ldebug(getppid))
1475 		printf(ARGS(getppid, ""));
1476 #endif
1477 
1478 	if (!linux_use26(td)) {
1479 		PROC_LOCK(td->td_proc);
1480 		td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1481 		PROC_UNLOCK(td->td_proc);
1482 		return (0);
1483 	}
1484 
1485 	em = em_find(td->td_proc, EMUL_DONTLOCK);
1486 
1487 	KASSERT(em != NULL, ("getppid: process emuldata not found.\n"));
1488 
1489 	/* find the group leader */
1490 	p = pfind(em->shared->group_pid);
1491 
1492 	if (p == NULL) {
1493 #ifdef DEBUG
1494 	   	printf(LMSG("parent process not found.\n"));
1495 #endif
1496 		return (0);
1497 	}
1498 
1499 	pp = p->p_pptr;		/* switch to parent */
1500 	PROC_LOCK(pp);
1501 	PROC_UNLOCK(p);
1502 
1503 	/* if its also linux process */
1504 	if (pp->p_sysent == &elf_linux_sysvec) {
1505 		em = em_find(pp, EMUL_DONTLOCK);
1506 		KASSERT(em != NULL, ("getppid: parent emuldata not found.\n"));
1507 
1508 		td->td_retval[0] = em->shared->group_pid;
1509 	} else
1510 		td->td_retval[0] = pp->p_pid;
1511 
1512 	PROC_UNLOCK(pp);
1513 
1514 	return (0);
1515 }
1516 
1517 int
1518 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1519 {
1520 
1521 #ifdef DEBUG
1522 	if (ldebug(getgid))
1523 		printf(ARGS(getgid, ""));
1524 #endif
1525 
1526 	td->td_retval[0] = td->td_ucred->cr_rgid;
1527 	return (0);
1528 }
1529 
1530 int
1531 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1532 {
1533 
1534 #ifdef DEBUG
1535 	if (ldebug(getuid))
1536 		printf(ARGS(getuid, ""));
1537 #endif
1538 
1539 	td->td_retval[0] = td->td_ucred->cr_ruid;
1540 	return (0);
1541 }
1542 
1543 
1544 int
1545 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1546 {
1547 	struct getsid_args bsd;
1548 
1549 #ifdef DEBUG
1550 	if (ldebug(getsid))
1551 		printf(ARGS(getsid, "%i"), args->pid);
1552 #endif
1553 
1554 	bsd.pid = args->pid;
1555 	return getsid(td, &bsd);
1556 }
1557 
1558 int
1559 linux_nosys(struct thread *td, struct nosys_args *ignore)
1560 {
1561 
1562 	return (ENOSYS);
1563 }
1564 
1565 int
1566 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1567 {
1568 	struct getpriority_args bsd_args;
1569 	int error;
1570 
1571 #ifdef DEBUG
1572 	if (ldebug(getpriority))
1573 		printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1574 #endif
1575 
1576 	bsd_args.which = args->which;
1577 	bsd_args.who = args->who;
1578 	error = getpriority(td, &bsd_args);
1579 	td->td_retval[0] = 20 - td->td_retval[0];
1580 	return error;
1581 }
1582 
1583 int
1584 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1585 {
1586 	int name[2];
1587 
1588 #ifdef DEBUG
1589 	if (ldebug(sethostname))
1590 		printf(ARGS(sethostname, "*, %i"), args->len);
1591 #endif
1592 
1593 	name[0] = CTL_KERN;
1594 	name[1] = KERN_HOSTNAME;
1595 	return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1596 	    args->len, 0, 0));
1597 }
1598 
1599 int
1600 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1601 {
1602 	struct linux_emuldata *em, *td_em, *tmp_em;
1603 	struct proc *sp;
1604 
1605 #ifdef DEBUG
1606 	if (ldebug(exit_group))
1607 		printf(ARGS(exit_group, "%i"), args->error_code);
1608 #endif
1609 
1610 	if (linux_use26(td)) {
1611 		td_em = em_find(td->td_proc, EMUL_DONTLOCK);
1612 
1613 		KASSERT(td_em != NULL, ("exit_group: emuldata not found.\n"));
1614 
1615 		EMUL_SHARED_RLOCK(&emul_shared_lock);
1616 		LIST_FOREACH_SAFE(em, &td_em->shared->threads, threads, tmp_em) {
1617 			if (em->pid == td_em->pid)
1618 				continue;
1619 
1620 			sp = pfind(em->pid);
1621 			psignal(sp, SIGKILL);
1622 			PROC_UNLOCK(sp);
1623 #ifdef DEBUG
1624 			printf(LMSG("linux_sys_exit_group: kill PID %d\n"), em->pid);
1625 #endif
1626 		}
1627 
1628 		EMUL_SHARED_RUNLOCK(&emul_shared_lock);
1629 	}
1630 	/*
1631 	 * XXX: we should send a signal to the parent if
1632 	 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1633 	 * as it doesnt occur often.
1634 	 */
1635 	exit1(td, W_EXITCODE(args->error_code, 0));
1636 
1637 	return (0);
1638 }
1639 
1640 int
1641 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1642 {
1643 	int error = 0, max_size;
1644 	struct proc *p = td->td_proc;
1645 	char comm[LINUX_MAX_COMM_LEN];
1646 	struct linux_emuldata *em;
1647 	int pdeath_signal;
1648 
1649 #ifdef DEBUG
1650 	if (ldebug(prctl))
1651 		printf(ARGS(prctl, "%d, %d, %d, %d, %d"), args->option,
1652 		    args->arg2, args->arg3, args->arg4, args->arg5);
1653 #endif
1654 
1655 	switch (args->option) {
1656 	case LINUX_PR_SET_PDEATHSIG:
1657 		if (!LINUX_SIG_VALID(args->arg2))
1658 			return (EINVAL);
1659 		em = em_find(p, EMUL_DOLOCK);
1660 		KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1661 		em->pdeath_signal = args->arg2;
1662 		EMUL_UNLOCK(&emul_lock);
1663 		break;
1664 	case LINUX_PR_GET_PDEATHSIG:
1665 		em = em_find(p, EMUL_DOLOCK);
1666 		KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1667 		pdeath_signal = em->pdeath_signal;
1668 		EMUL_UNLOCK(&emul_lock);
1669 		error = copyout(&pdeath_signal,
1670 		    (void *)(register_t)args->arg2,
1671 		    sizeof(pdeath_signal));
1672 		break;
1673 	case LINUX_PR_SET_NAME:
1674 		/*
1675 		 * To be on the safe side we need to make sure to not
1676 		 * overflow the size a linux program expects. We already
1677 		 * do this here in the copyin, so that we don't need to
1678 		 * check on copyout.
1679 		 */
1680 		max_size = MIN(sizeof(comm), sizeof(p->p_comm));
1681 		error = copyinstr((void *)(register_t)args->arg2, comm,
1682 		    max_size, NULL);
1683 
1684 		/* Linux silently truncates the name if it is too long. */
1685 		if (error == ENAMETOOLONG) {
1686 			/*
1687 			 * XXX: copyinstr() isn't documented to populate the
1688 			 * array completely, so do a copyin() to be on the
1689 			 * safe side. This should be changed in case
1690 			 * copyinstr() is changed to guarantee this.
1691 			 */
1692 			error = copyin((void *)(register_t)args->arg2, comm,
1693 			    max_size - 1);
1694 			comm[max_size - 1] = '\0';
1695 		}
1696 		if (error)
1697 			return (error);
1698 
1699 		PROC_LOCK(p);
1700 		strlcpy(p->p_comm, comm, sizeof(p->p_comm));
1701 		PROC_UNLOCK(p);
1702 		break;
1703 	case LINUX_PR_GET_NAME:
1704 		PROC_LOCK(p);
1705 		strlcpy(comm, p->p_comm, sizeof(comm));
1706 		PROC_UNLOCK(p);
1707 		error = copyout(comm, (void *)(register_t)args->arg2,
1708 		    strlen(comm) + 1);
1709 		break;
1710 	default:
1711 		error = EINVAL;
1712 		break;
1713 	}
1714 
1715 	return (error);
1716 }
1717