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