xref: /freebsd/sys/compat/linux/linux_misc.c (revision a163d034fadcfb4a25ca34a2ba5f491c47b6ff69)
1 /*-
2  * Copyright (c) 1994-1995 S�ren Schmidt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include "opt_mac.h"
32 
33 #include <sys/param.h>
34 #include <sys/blist.h>
35 #include <sys/fcntl.h>
36 #include <sys/imgact_aout.h>
37 #include <sys/jail.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/mac.h>
41 #include <sys/malloc.h>
42 #include <sys/mman.h>
43 #include <sys/mount.h>
44 #include <sys/mutex.h>
45 #include <sys/namei.h>
46 #include <sys/proc.h>
47 #include <sys/reboot.h>
48 #include <sys/resourcevar.h>
49 #include <sys/signalvar.h>
50 #include <sys/stat.h>
51 #include <sys/syscallsubr.h>
52 #include <sys/sysctl.h>
53 #include <sys/sysproto.h>
54 #include <sys/systm.h>
55 #include <sys/time.h>
56 #include <sys/vmmeter.h>
57 #include <sys/vnode.h>
58 #include <sys/wait.h>
59 
60 #include <vm/vm.h>
61 #include <vm/pmap.h>
62 #include <vm/vm_kern.h>
63 #include <vm/vm_map.h>
64 #include <vm/vm_extern.h>
65 #include <vm/vm_object.h>
66 #include <vm/swap_pager.h>
67 
68 #include <machine/limits.h>
69 
70 #include <posix4/sched.h>
71 
72 #include <machine/../linux/linux.h>
73 #include <machine/../linux/linux_proto.h>
74 
75 #include <compat/linux/linux_mib.h>
76 #include <compat/linux/linux_util.h>
77 
78 #ifdef __alpha__
79 #define BSD_TO_LINUX_SIGNAL(sig)       (sig)
80 #else
81 #define BSD_TO_LINUX_SIGNAL(sig)	\
82 	(((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
83 #endif
84 
85 #ifndef __alpha__
86 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
87 	RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
88 	RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
89 	RLIMIT_MEMLOCK, -1
90 };
91 #endif /*!__alpha__*/
92 
93 struct l_sysinfo {
94 	l_long		uptime;		/* Seconds since boot */
95 	l_ulong		loads[3];	/* 1, 5, and 15 minute load averages */
96 	l_ulong		totalram;	/* Total usable main memory size */
97 	l_ulong		freeram;	/* Available memory size */
98 	l_ulong		sharedram;	/* Amount of shared memory */
99 	l_ulong		bufferram;	/* Memory used by buffers */
100 	l_ulong		totalswap;	/* Total swap space size */
101 	l_ulong		freeswap;	/* swap space still available */
102 	l_ushort	procs;		/* Number of current processes */
103 	char		_f[22];		/* Pads structure to 64 bytes */
104 };
105 #ifndef __alpha__
106 int
107 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
108 {
109 	struct l_sysinfo sysinfo;
110 	vm_object_t object;
111 	int i;
112 	struct timespec ts;
113 
114 	/* Uptime is copied out of print_uptime() in kern_shutdown.c */
115 	getnanouptime(&ts);
116 	i = 0;
117 	if (ts.tv_sec >= 86400) {
118 		ts.tv_sec %= 86400;
119 		i = 1;
120 	}
121 	if (i || ts.tv_sec >= 3600) {
122 		ts.tv_sec %= 3600;
123 		i = 1;
124 	}
125 	if (i || ts.tv_sec >= 60) {
126 		ts.tv_sec %= 60;
127 		i = 1;
128 	}
129 	sysinfo.uptime=ts.tv_sec;
130 
131 	/* Use the information from the mib to get our load averages */
132 	for (i = 0; i < 3; i++)
133 		sysinfo.loads[i] = averunnable.ldavg[i];
134 
135 	sysinfo.totalram = physmem * PAGE_SIZE;
136 	sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
137 
138 	sysinfo.sharedram = 0;
139 	for (object = TAILQ_FIRST(&vm_object_list); object != NULL;
140 	     object = TAILQ_NEXT(object, object_list))
141 		if (object->shadow_count > 1)
142 			sysinfo.sharedram += object->resident_page_count;
143 
144 	sysinfo.sharedram *= PAGE_SIZE;
145 	sysinfo.bufferram = 0;
146 
147 	if (swapblist == NULL) {
148 		sysinfo.totalswap= 0;
149 		sysinfo.freeswap = 0;
150 	} else {
151 		sysinfo.totalswap = swapblist->bl_blocks * 1024;
152 		sysinfo.freeswap = swapblist->bl_root->u.bmu_avail * PAGE_SIZE;
153 	}
154 
155 	sysinfo.procs = 20; /* Hack */
156 
157 	return copyout(&sysinfo, (caddr_t)args->info, sizeof(sysinfo));
158 }
159 #endif /*!__alpha__*/
160 
161 #ifndef __alpha__
162 int
163 linux_alarm(struct thread *td, struct linux_alarm_args *args)
164 {
165 	struct itimerval it, old_it;
166 	struct timeval tv;
167 
168 #ifdef DEBUG
169 	if (ldebug(alarm))
170 		printf(ARGS(alarm, "%u"), args->secs);
171 #endif
172 
173 	if (args->secs > 100000000)
174 		return EINVAL;
175 
176 	it.it_value.tv_sec = (long)args->secs;
177 	it.it_value.tv_usec = 0;
178 	it.it_interval.tv_sec = 0;
179 	it.it_interval.tv_usec = 0;
180 	PROC_LOCK(td->td_proc);
181 	old_it = td->td_proc->p_realtimer;
182 	getmicrouptime(&tv);
183 	if (timevalisset(&old_it.it_value))
184 		callout_stop(&td->td_proc->p_itcallout);
185 	if (it.it_value.tv_sec != 0) {
186 		callout_reset(&td->td_proc->p_itcallout, tvtohz(&it.it_value),
187 		    realitexpire, td->td_proc);
188 		timevaladd(&it.it_value, &tv);
189 	}
190 	td->td_proc->p_realtimer = it;
191 	PROC_UNLOCK(td->td_proc);
192 	if (timevalcmp(&old_it.it_value, &tv, >)) {
193 		timevalsub(&old_it.it_value, &tv);
194 		if (old_it.it_value.tv_usec != 0)
195 			old_it.it_value.tv_sec++;
196 		td->td_retval[0] = old_it.it_value.tv_sec;
197 	}
198 	return 0;
199 }
200 #endif /*!__alpha__*/
201 
202 int
203 linux_brk(struct thread *td, struct linux_brk_args *args)
204 {
205 	struct vmspace *vm = td->td_proc->p_vmspace;
206 	vm_offset_t new, old;
207 	struct obreak_args /* {
208 		char * nsize;
209 	} */ tmp;
210 
211 #ifdef DEBUG
212 	if (ldebug(brk))
213 		printf(ARGS(brk, "%p"), (void *)args->dsend);
214 #endif
215 	old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
216 	new = (vm_offset_t)args->dsend;
217 	tmp.nsize = (char *) new;
218 	if (((caddr_t)new > vm->vm_daddr) && !obreak(td, &tmp))
219 		td->td_retval[0] = (long)new;
220 	else
221 		td->td_retval[0] = (long)old;
222 
223 	return 0;
224 }
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;
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 	locked = 0;
250 	vp = NULL;
251 
252 	/*
253 	 * XXX: This code should make use of vn_open(), rather than doing
254 	 * all this stuff itself.
255 	 */
256 	NDINIT(&ni, LOOKUP, FOLLOW|LOCKLEAF, UIO_SYSSPACE, library, td);
257 	error = namei(&ni);
258 	LFREEPATH(library);
259 	if (error)
260 		goto cleanup;
261 
262 	vp = ni.ni_vp;
263 	/*
264 	 * XXX - This looks like a bogus check. A LOCKLEAF namei should not
265 	 * succeed without returning a vnode.
266 	 */
267 	if (vp == NULL) {
268 		error = ENOEXEC;	/* ?? */
269 		goto cleanup;
270 	}
271 	NDFREE(&ni, NDF_ONLY_PNBUF);
272 
273 	/*
274 	 * From here on down, we have a locked vnode that must be unlocked.
275 	 */
276 	locked++;
277 
278 	/* Writable? */
279 	if (vp->v_writecount) {
280 		error = ETXTBSY;
281 		goto cleanup;
282 	}
283 
284 	/* Executable? */
285 	error = VOP_GETATTR(vp, &attr, td->td_ucred, td);
286 	if (error)
287 		goto cleanup;
288 
289 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
290 	    ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
291 		error = ENOEXEC;
292 		goto cleanup;
293 	}
294 
295 	/* Sensible size? */
296 	if (attr.va_size == 0) {
297 		error = ENOEXEC;
298 		goto cleanup;
299 	}
300 
301 	/* Can we access it? */
302 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
303 	if (error)
304 		goto cleanup;
305 
306 	/*
307 	 * XXX: This should use vn_open() so that it is properly authorized,
308 	 * and to reduce code redundancy all over the place here.
309 	 */
310 #ifdef MAC
311 	error = mac_check_vnode_open(td->td_ucred, vp, FREAD);
312 	if (error)
313 		goto cleanup;
314 #endif
315 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td);
316 	if (error)
317 		goto cleanup;
318 
319 	/* Pull in executable header into kernel_map */
320 	error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
321 	    VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0);
322 	/*
323 	 * Lock no longer needed
324 	 */
325 	locked = 0;
326 	VOP_UNLOCK(vp, 0, td);
327 
328 	if (error)
329 		goto cleanup;
330 
331 	/* Is it a Linux binary ? */
332 	if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
333 		error = ENOEXEC;
334 		goto cleanup;
335 	}
336 
337 	/*
338 	 * While we are here, we should REALLY do some more checks
339 	 */
340 
341 	/* Set file/virtual offset based on a.out variant. */
342 	switch ((int)(a_out->a_magic & 0xffff)) {
343 	case 0413:	/* ZMAGIC */
344 		file_offset = 1024;
345 		break;
346 	case 0314:	/* QMAGIC */
347 		file_offset = 0;
348 		break;
349 	default:
350 		error = ENOEXEC;
351 		goto cleanup;
352 	}
353 
354 	bss_size = round_page(a_out->a_bss);
355 
356 	/* Check various fields in header for validity/bounds. */
357 	if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
358 		error = ENOEXEC;
359 		goto cleanup;
360 	}
361 
362 	/* text + data can't exceed file size */
363 	if (a_out->a_data + a_out->a_text > attr.va_size) {
364 		error = EFAULT;
365 		goto cleanup;
366 	}
367 
368 	/* To protect td->td_proc->p_rlimit in the if condition. */
369 	mtx_assert(&Giant, MA_OWNED);
370 
371 	/*
372 	 * text/data/bss must not exceed limits
373 	 * XXX - this is not complete. it should check current usage PLUS
374 	 * the resources needed by this library.
375 	 */
376 	if (a_out->a_text > maxtsiz ||
377 	    a_out->a_data + bss_size >
378 	    td->td_proc->p_rlimit[RLIMIT_DATA].rlim_cur) {
379 		error = ENOMEM;
380 		goto cleanup;
381 	}
382 
383 	mp_fixme("Unlocked vflags access.");
384 	/* prevent more writers */
385 	vp->v_vflag |= VV_TEXT;
386 
387 	/*
388 	 * Check if file_offset page aligned. Currently we cannot handle
389 	 * misalinged file offsets, and so we read in the entire image
390 	 * (what a waste).
391 	 */
392 	if (file_offset & PAGE_MASK) {
393 #ifdef DEBUG
394 		printf("uselib: Non page aligned binary %lu\n", file_offset);
395 #endif
396 		/* Map text+data read/write/execute */
397 
398 		/* a_entry is the load address and is page aligned */
399 		vmaddr = trunc_page(a_out->a_entry);
400 
401 		/* get anon user mapping, read+write+execute */
402 		error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
403 		    &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
404 		    VM_PROT_ALL, 0);
405 		if (error)
406 			goto cleanup;
407 
408 		/* map file into kernel_map */
409 		error = vm_mmap(kernel_map, &buffer,
410 		    round_page(a_out->a_text + a_out->a_data + file_offset),
411 		    VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp,
412 		    trunc_page(file_offset));
413 		if (error)
414 			goto cleanup;
415 
416 		/* copy from kernel VM space to user space */
417 		error = copyout((caddr_t)(uintptr_t)(buffer + file_offset),
418 		    (caddr_t)vmaddr, a_out->a_text + a_out->a_data);
419 
420 		/* release temporary kernel space */
421 		vm_map_remove(kernel_map, buffer, buffer +
422 		    round_page(a_out->a_text + a_out->a_data + file_offset));
423 
424 		if (error)
425 			goto cleanup;
426 	} else {
427 #ifdef DEBUG
428 		printf("uselib: Page aligned binary %lu\n", file_offset);
429 #endif
430 		/*
431 		 * for QMAGIC, a_entry is 20 bytes beyond the load address
432 		 * to skip the executable header
433 		 */
434 		vmaddr = trunc_page(a_out->a_entry);
435 
436 		/*
437 		 * Map it all into the process's space as a single
438 		 * copy-on-write "data" segment.
439 		 */
440 		error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
441 		    a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
442 		    MAP_PRIVATE | MAP_FIXED, (caddr_t)vp, file_offset);
443 		if (error)
444 			goto cleanup;
445 	}
446 #ifdef DEBUG
447 	printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
448 	    ((long*)vmaddr)[1]);
449 #endif
450 	if (bss_size != 0) {
451 		/* Calculate BSS start address */
452 		vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
453 		    a_out->a_data;
454 
455 		/* allocate some 'anon' space */
456 		error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
457 		    &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
458 		if (error)
459 			goto cleanup;
460 	}
461 
462 cleanup:
463 	/* Unlock vnode if needed */
464 	if (locked)
465 		VOP_UNLOCK(vp, 0, td);
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 int
476 linux_select(struct thread *td, struct linux_select_args *args)
477 {
478 	struct timeval tv0, tv1, utv, *tvp;
479 	int error;
480 
481 #ifdef DEBUG
482 	if (ldebug(select))
483 		printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
484 		    (void *)args->readfds, (void *)args->writefds,
485 		    (void *)args->exceptfds, (void *)args->timeout);
486 #endif
487 
488 	/*
489 	 * Store current time for computation of the amount of
490 	 * time left.
491 	 */
492 	if (args->timeout) {
493 		if ((error = copyin((caddr_t)args->timeout, &utv,
494 		    sizeof(utv))))
495 			goto select_out;
496 #ifdef DEBUG
497 		if (ldebug(select))
498 			printf(LMSG("incoming timeout (%ld/%ld)"),
499 			    utv.tv_sec, utv.tv_usec);
500 #endif
501 
502 		if (itimerfix(&utv)) {
503 			/*
504 			 * The timeval was invalid.  Convert it to something
505 			 * valid that will act as it does under Linux.
506 			 */
507 			utv.tv_sec += utv.tv_usec / 1000000;
508 			utv.tv_usec %= 1000000;
509 			if (utv.tv_usec < 0) {
510 				utv.tv_sec -= 1;
511 				utv.tv_usec += 1000000;
512 			}
513 			if (utv.tv_sec < 0)
514 				timevalclear(&utv);
515 		}
516 		microtime(&tv0);
517 		tvp = &utv;
518 	} else
519 		tvp = NULL;
520 
521 	error = kern_select(td, args->nfds, args->readfds, args->writefds,
522 	    args->exceptfds, tvp);
523 
524 #ifdef DEBUG
525 	if (ldebug(select))
526 		printf(LMSG("real select returns %d"), error);
527 #endif
528 	if (error) {
529 		/*
530 		 * See fs/select.c in the Linux kernel.  Without this,
531 		 * Maelstrom doesn't work.
532 		 */
533 		if (error == ERESTART)
534 			error = EINTR;
535 		goto select_out;
536 	}
537 
538 	if (args->timeout) {
539 		if (td->td_retval[0]) {
540 			/*
541 			 * Compute how much time was left of the timeout,
542 			 * by subtracting the current time and the time
543 			 * before we started the call, and subtracting
544 			 * that result from the user-supplied value.
545 			 */
546 			microtime(&tv1);
547 			timevalsub(&tv1, &tv0);
548 			timevalsub(&utv, &tv1);
549 			if (utv.tv_sec < 0)
550 				timevalclear(&utv);
551 		} else
552 			timevalclear(&utv);
553 #ifdef DEBUG
554 		if (ldebug(select))
555 			printf(LMSG("outgoing timeout (%ld/%ld)"),
556 			    utv.tv_sec, utv.tv_usec);
557 #endif
558 		if ((error = copyout(&utv, (caddr_t)args->timeout,
559 		    sizeof(utv))))
560 			goto select_out;
561 	}
562 
563 select_out:
564 #ifdef DEBUG
565 	if (ldebug(select))
566 		printf(LMSG("select_out -> %d"), error);
567 #endif
568 	return error;
569 }
570 
571 int
572 linux_mremap(struct thread *td, struct linux_mremap_args *args)
573 {
574 	struct munmap_args /* {
575 		void *addr;
576 		size_t len;
577 	} */ bsd_args;
578 	int error = 0;
579 
580 #ifdef DEBUG
581 	if (ldebug(mremap))
582 		printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
583 		    (void *)args->addr,
584 		    (unsigned long)args->old_len,
585 		    (unsigned long)args->new_len,
586 		    (unsigned long)args->flags);
587 #endif
588 	args->new_len = round_page(args->new_len);
589 	args->old_len = round_page(args->old_len);
590 
591 	if (args->new_len > args->old_len) {
592 		td->td_retval[0] = 0;
593 		return ENOMEM;
594 	}
595 
596 	if (args->new_len < args->old_len) {
597 		bsd_args.addr = (caddr_t)(args->addr + args->new_len);
598 		bsd_args.len = args->old_len - args->new_len;
599 		error = munmap(td, &bsd_args);
600 	}
601 
602 	td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
603 	return error;
604 }
605 
606 #define LINUX_MS_ASYNC       0x0001
607 #define LINUX_MS_INVALIDATE  0x0002
608 #define LINUX_MS_SYNC        0x0004
609 
610 int
611 linux_msync(struct thread *td, struct linux_msync_args *args)
612 {
613 	struct msync_args bsd_args;
614 
615 	bsd_args.addr = (caddr_t)args->addr;
616 	bsd_args.len = args->len;
617 	bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
618 
619 	return msync(td, &bsd_args);
620 }
621 
622 #ifndef __alpha__
623 int
624 linux_time(struct thread *td, struct linux_time_args *args)
625 {
626 	struct timeval tv;
627 	l_time_t tm;
628 	int error;
629 
630 #ifdef DEBUG
631 	if (ldebug(time))
632 		printf(ARGS(time, "*"));
633 #endif
634 
635 	microtime(&tv);
636 	tm = tv.tv_sec;
637 	if (args->tm && (error = copyout(&tm, (caddr_t)args->tm, sizeof(tm))))
638 		return error;
639 	td->td_retval[0] = tm;
640 	return 0;
641 }
642 #endif	/*!__alpha__*/
643 
644 struct l_times_argv {
645 	l_long		tms_utime;
646 	l_long		tms_stime;
647 	l_long		tms_cutime;
648 	l_long		tms_cstime;
649 };
650 
651 #ifdef __alpha__
652 #define CLK_TCK 1024	/* Linux uses 1024 on alpha */
653 #else
654 #define CLK_TCK 100	/* Linux uses 100 */
655 #endif
656 
657 #define CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
658 
659 int
660 linux_times(struct thread *td, struct linux_times_args *args)
661 {
662 	struct timeval tv;
663 	struct l_times_argv tms;
664 	struct rusage ru;
665 	int error;
666 
667 #ifdef DEBUG
668 	if (ldebug(times))
669 		printf(ARGS(times, "*"));
670 #endif
671 
672 	mtx_lock_spin(&sched_lock);
673 	calcru(td->td_proc, &ru.ru_utime, &ru.ru_stime, NULL);
674 	mtx_unlock_spin(&sched_lock);
675 
676 	tms.tms_utime = CONVTCK(ru.ru_utime);
677 	tms.tms_stime = CONVTCK(ru.ru_stime);
678 
679 	tms.tms_cutime = CONVTCK(td->td_proc->p_stats->p_cru.ru_utime);
680 	tms.tms_cstime = CONVTCK(td->td_proc->p_stats->p_cru.ru_stime);
681 
682 	if ((error = copyout(&tms, (caddr_t)args->buf, sizeof(tms))))
683 		return error;
684 
685 	microuptime(&tv);
686 	td->td_retval[0] = (int)CONVTCK(tv);
687 	return 0;
688 }
689 
690 int
691 linux_newuname(struct thread *td, struct linux_newuname_args *args)
692 {
693 	struct l_new_utsname utsname;
694 	char osname[LINUX_MAX_UTSNAME];
695 	char osrelease[LINUX_MAX_UTSNAME];
696 
697 #ifdef DEBUG
698 	if (ldebug(newuname))
699 		printf(ARGS(newuname, "*"));
700 #endif
701 
702 	linux_get_osname(td->td_proc, osname);
703 	linux_get_osrelease(td->td_proc, osrelease);
704 
705 	bzero(&utsname, sizeof(utsname));
706 	strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
707 	getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
708 	strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
709 	strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
710 	strlcpy(utsname.machine, machine, LINUX_MAX_UTSNAME);
711 	strlcpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME);
712 
713 	return (copyout(&utsname, (caddr_t)args->buf, sizeof(utsname)));
714 }
715 
716 #if defined(__i386__)
717 struct l_utimbuf {
718 	l_time_t l_actime;
719 	l_time_t l_modtime;
720 };
721 
722 int
723 linux_utime(struct thread *td, struct linux_utime_args *args)
724 {
725 	struct timeval tv[2], *tvp;
726 	struct l_utimbuf lut;
727 	char *fname;
728 	int error;
729 
730 	LCONVPATHEXIST(td, args->fname, &fname);
731 
732 #ifdef DEBUG
733 	if (ldebug(utime))
734 		printf(ARGS(utime, "%s, *"), fname);
735 #endif
736 
737 	if (args->times) {
738 		if ((error = copyin((caddr_t)args->times, &lut, sizeof lut))) {
739 			LFREEPATH(fname);
740 			return error;
741 		}
742 		tv[0].tv_sec = lut.l_actime;
743 		tv[0].tv_usec = 0;
744 		tv[1].tv_sec = lut.l_modtime;
745 		tv[1].tv_usec = 0;
746 		tvp = tv;
747 	} else
748 		tvp = NULL;
749 
750 	error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
751 	LFREEPATH(fname);
752 	return (error);
753 }
754 #endif /* __i386__ */
755 
756 #define __WCLONE 0x80000000
757 
758 #ifndef __alpha__
759 int
760 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
761 {
762 	struct wait_args /* {
763 		int pid;
764 		int *status;
765 		int options;
766 		struct	rusage *rusage;
767 	} */ tmp;
768 	int error, tmpstat;
769 
770 #ifdef DEBUG
771 	if (ldebug(waitpid))
772 		printf(ARGS(waitpid, "%d, %p, %d"),
773 		    args->pid, (void *)args->status, args->options);
774 #endif
775 
776 	tmp.pid = args->pid;
777 	tmp.status = args->status;
778 	tmp.options = (args->options & (WNOHANG | WUNTRACED));
779 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
780 	if (args->options & __WCLONE)
781 		tmp.options |= WLINUXCLONE;
782 	tmp.rusage = NULL;
783 
784 	if ((error = wait4(td, &tmp)) != 0)
785 		return error;
786 
787 	if (args->status) {
788 		if ((error = copyin((caddr_t)args->status, &tmpstat,
789 		    sizeof(int))) != 0)
790 			return error;
791 		tmpstat &= 0xffff;
792 		if (WIFSIGNALED(tmpstat))
793 			tmpstat = (tmpstat & 0xffffff80) |
794 			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
795 		else if (WIFSTOPPED(tmpstat))
796 			tmpstat = (tmpstat & 0xffff00ff) |
797 			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
798 		return copyout(&tmpstat, (caddr_t)args->status, sizeof(int));
799 	}
800 
801 	return 0;
802 }
803 #endif	/*!__alpha__*/
804 
805 int
806 linux_wait4(struct thread *td, struct linux_wait4_args *args)
807 {
808 	struct wait_args /* {
809 		int pid;
810 		int *status;
811 		int options;
812 		struct	rusage *rusage;
813 	} */ tmp;
814 	int error, tmpstat;
815 
816 #ifdef DEBUG
817 	if (ldebug(wait4))
818 		printf(ARGS(wait4, "%d, %p, %d, %p"),
819 		    args->pid, (void *)args->status, args->options,
820 		    (void *)args->rusage);
821 #endif
822 
823 	tmp.pid = args->pid;
824 	tmp.status = args->status;
825 	tmp.options = (args->options & (WNOHANG | WUNTRACED));
826 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
827 	if (args->options & __WCLONE)
828 		tmp.options |= WLINUXCLONE;
829 	tmp.rusage = (struct rusage *)args->rusage;
830 
831 	if ((error = wait4(td, &tmp)) != 0)
832 		return error;
833 
834 	PROC_LOCK(td->td_proc);
835 	SIGDELSET(td->td_proc->p_siglist, SIGCHLD);
836 	PROC_UNLOCK(td->td_proc);
837 
838 	if (args->status) {
839 		if ((error = copyin((caddr_t)args->status, &tmpstat,
840 		    sizeof(int))) != 0)
841 			return error;
842 		tmpstat &= 0xffff;
843 		if (WIFSIGNALED(tmpstat))
844 			tmpstat = (tmpstat & 0xffffff80) |
845 			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
846 		else if (WIFSTOPPED(tmpstat))
847 			tmpstat = (tmpstat & 0xffff00ff) |
848 			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
849 		return copyout(&tmpstat, (caddr_t)args->status, sizeof(int));
850 	}
851 
852 	return 0;
853 }
854 
855 int
856 linux_mknod(struct thread *td, struct linux_mknod_args *args)
857 {
858 	char *path;
859 	int error;
860 
861 	LCONVPATHCREAT(td, args->path, &path);
862 
863 #ifdef DEBUG
864 	if (ldebug(mknod))
865 		printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
866 #endif
867 
868 	if (args->mode & S_IFIFO)
869 		error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
870 	else
871 		error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
872 		    args->dev);
873 	LFREEPATH(path);
874 	return (error);
875 }
876 
877 /*
878  * UGH! This is just about the dumbest idea I've ever heard!!
879  */
880 int
881 linux_personality(struct thread *td, struct linux_personality_args *args)
882 {
883 #ifdef DEBUG
884 	if (ldebug(personality))
885 		printf(ARGS(personality, "%d"), args->per);
886 #endif
887 #ifndef __alpha__
888 	if (args->per != 0)
889 		return EINVAL;
890 #endif
891 
892 	/* Yes Jim, it's still a Linux... */
893 	td->td_retval[0] = 0;
894 	return 0;
895 }
896 
897 /*
898  * Wrappers for get/setitimer for debugging..
899  */
900 int
901 linux_setitimer(struct thread *td, struct linux_setitimer_args *args)
902 {
903 	struct setitimer_args bsa;
904 	struct itimerval foo;
905 	int error;
906 
907 #ifdef DEBUG
908 	if (ldebug(setitimer))
909 		printf(ARGS(setitimer, "%p, %p"),
910 		    (void *)args->itv, (void *)args->oitv);
911 #endif
912 	bsa.which = args->which;
913 	bsa.itv = (struct itimerval *)args->itv;
914 	bsa.oitv = (struct itimerval *)args->oitv;
915 	if (args->itv) {
916 	    if ((error = copyin((caddr_t)args->itv, &foo, sizeof(foo))))
917 		return error;
918 #ifdef DEBUG
919 	    if (ldebug(setitimer)) {
920 	        printf("setitimer: value: sec: %ld, usec: %ld\n",
921 		    foo.it_value.tv_sec, foo.it_value.tv_usec);
922 	        printf("setitimer: interval: sec: %ld, usec: %ld\n",
923 		    foo.it_interval.tv_sec, foo.it_interval.tv_usec);
924 	    }
925 #endif
926 	}
927 	return setitimer(td, &bsa);
928 }
929 
930 int
931 linux_getitimer(struct thread *td, struct linux_getitimer_args *args)
932 {
933 	struct getitimer_args bsa;
934 #ifdef DEBUG
935 	if (ldebug(getitimer))
936 		printf(ARGS(getitimer, "%p"), (void *)args->itv);
937 #endif
938 	bsa.which = args->which;
939 	bsa.itv = (struct itimerval *)args->itv;
940 	return getitimer(td, &bsa);
941 }
942 
943 #ifndef __alpha__
944 int
945 linux_nice(struct thread *td, struct linux_nice_args *args)
946 {
947 	struct setpriority_args	bsd_args;
948 
949 	bsd_args.which = PRIO_PROCESS;
950 	bsd_args.who = 0;	/* current process */
951 	bsd_args.prio = args->inc;
952 	return setpriority(td, &bsd_args);
953 }
954 #endif	/*!__alpha__*/
955 
956 int
957 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
958 {
959 	struct ucred *newcred, *oldcred;
960 	l_gid_t linux_gidset[NGROUPS];
961 	gid_t *bsd_gidset;
962 	int ngrp, error;
963 	struct proc *p;
964 
965 	ngrp = args->gidsetsize;
966 	if (ngrp >= NGROUPS)
967 		return (EINVAL);
968 	error = copyin((caddr_t)args->grouplist, linux_gidset,
969 	    ngrp * sizeof(l_gid_t));
970 	if (error)
971 		return (error);
972 	newcred = crget();
973 	p = td->td_proc;
974 	PROC_LOCK(p);
975 	oldcred = p->p_ucred;
976 
977 	/*
978 	 * cr_groups[0] holds egid. Setting the whole set from
979 	 * the supplied set will cause egid to be changed too.
980 	 * Keep cr_groups[0] unchanged to prevent that.
981 	 */
982 
983 	if ((error = suser_cred(oldcred, PRISON_ROOT)) != 0) {
984 		PROC_UNLOCK(p);
985 		crfree(newcred);
986 		return (error);
987 	}
988 
989 	crcopy(newcred, oldcred);
990 	if (ngrp > 0) {
991 		newcred->cr_ngroups = ngrp + 1;
992 
993 		bsd_gidset = newcred->cr_groups;
994 		ngrp--;
995 		while (ngrp >= 0) {
996 			bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
997 			ngrp--;
998 		}
999 	}
1000 	else
1001 		newcred->cr_ngroups = 1;
1002 
1003 	setsugid(p);
1004 	p->p_ucred = newcred;
1005 	PROC_UNLOCK(p);
1006 	crfree(oldcred);
1007 	return (0);
1008 }
1009 
1010 int
1011 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1012 {
1013 	struct ucred *cred;
1014 	l_gid_t linux_gidset[NGROUPS];
1015 	gid_t *bsd_gidset;
1016 	int bsd_gidsetsz, ngrp, error;
1017 
1018 	cred = td->td_ucred;
1019 	bsd_gidset = cred->cr_groups;
1020 	bsd_gidsetsz = cred->cr_ngroups - 1;
1021 
1022 	/*
1023 	 * cr_groups[0] holds egid. Returning the whole set
1024 	 * here will cause a duplicate. Exclude cr_groups[0]
1025 	 * to prevent that.
1026 	 */
1027 
1028 	if ((ngrp = args->gidsetsize) == 0) {
1029 		td->td_retval[0] = bsd_gidsetsz;
1030 		return (0);
1031 	}
1032 
1033 	if (ngrp < bsd_gidsetsz)
1034 		return (EINVAL);
1035 
1036 	ngrp = 0;
1037 	while (ngrp < bsd_gidsetsz) {
1038 		linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1039 		ngrp++;
1040 	}
1041 
1042 	if ((error = copyout(linux_gidset, (caddr_t)args->grouplist,
1043 	    ngrp * sizeof(l_gid_t))))
1044 		return (error);
1045 
1046 	td->td_retval[0] = ngrp;
1047 	return (0);
1048 }
1049 
1050 #ifndef __alpha__
1051 int
1052 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1053 {
1054 	struct rlimit bsd_rlim;
1055 	struct l_rlimit rlim;
1056 	u_int which;
1057 	int error;
1058 
1059 #ifdef DEBUG
1060 	if (ldebug(setrlimit))
1061 		printf(ARGS(setrlimit, "%d, %p"),
1062 		    args->resource, (void *)args->rlim);
1063 #endif
1064 
1065 	if (args->resource >= LINUX_RLIM_NLIMITS)
1066 		return (EINVAL);
1067 
1068 	which = linux_to_bsd_resource[args->resource];
1069 	if (which == -1)
1070 		return (EINVAL);
1071 
1072 	error = copyin((caddr_t)args->rlim, &rlim, sizeof(rlim));
1073 	if (error)
1074 		return (error);
1075 
1076 	bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1077 	bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1078 	return (dosetrlimit(td, which, &bsd_rlim));
1079 }
1080 
1081 int
1082 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1083 {
1084 	struct l_rlimit rlim;
1085 	struct proc *p = td->td_proc;
1086 	struct rlimit *bsd_rlp;
1087 	u_int which;
1088 
1089 #ifdef DEBUG
1090 	if (ldebug(old_getrlimit))
1091 		printf(ARGS(old_getrlimit, "%d, %p"),
1092 		    args->resource, (void *)args->rlim);
1093 #endif
1094 
1095 	if (args->resource >= LINUX_RLIM_NLIMITS)
1096 		return (EINVAL);
1097 
1098 	which = linux_to_bsd_resource[args->resource];
1099 	if (which == -1)
1100 		return (EINVAL);
1101 	bsd_rlp = &p->p_rlimit[which];
1102 
1103 	rlim.rlim_cur = (unsigned long)bsd_rlp->rlim_cur;
1104 	if (rlim.rlim_cur == ULONG_MAX)
1105 		rlim.rlim_cur = LONG_MAX;
1106 	rlim.rlim_max = (unsigned long)bsd_rlp->rlim_max;
1107 	if (rlim.rlim_max == ULONG_MAX)
1108 		rlim.rlim_max = LONG_MAX;
1109 	return (copyout(&rlim, (caddr_t)args->rlim, sizeof(rlim)));
1110 }
1111 
1112 int
1113 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1114 {
1115 	struct l_rlimit rlim;
1116 	struct proc *p = td->td_proc;
1117 	struct rlimit *bsd_rlp;
1118 	u_int which;
1119 
1120 #ifdef DEBUG
1121 	if (ldebug(getrlimit))
1122 		printf(ARGS(getrlimit, "%d, %p"),
1123 		    args->resource, (void *)args->rlim);
1124 #endif
1125 
1126 	if (args->resource >= LINUX_RLIM_NLIMITS)
1127 		return (EINVAL);
1128 
1129 	which = linux_to_bsd_resource[args->resource];
1130 	if (which == -1)
1131 		return (EINVAL);
1132 	bsd_rlp = &p->p_rlimit[which];
1133 
1134 	rlim.rlim_cur = (l_ulong)bsd_rlp->rlim_cur;
1135 	rlim.rlim_max = (l_ulong)bsd_rlp->rlim_max;
1136 	return (copyout(&rlim, (caddr_t)args->rlim, sizeof(rlim)));
1137 }
1138 #endif /*!__alpha__*/
1139 
1140 int
1141 linux_sched_setscheduler(struct thread *td,
1142     struct linux_sched_setscheduler_args *args)
1143 {
1144 	struct sched_setscheduler_args bsd;
1145 
1146 #ifdef DEBUG
1147 	if (ldebug(sched_setscheduler))
1148 		printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1149 		    args->pid, args->policy, (const void *)args->param);
1150 #endif
1151 
1152 	switch (args->policy) {
1153 	case LINUX_SCHED_OTHER:
1154 		bsd.policy = SCHED_OTHER;
1155 		break;
1156 	case LINUX_SCHED_FIFO:
1157 		bsd.policy = SCHED_FIFO;
1158 		break;
1159 	case LINUX_SCHED_RR:
1160 		bsd.policy = SCHED_RR;
1161 		break;
1162 	default:
1163 		return EINVAL;
1164 	}
1165 
1166 	bsd.pid = args->pid;
1167 	bsd.param = (struct sched_param *)args->param;
1168 	return sched_setscheduler(td, &bsd);
1169 }
1170 
1171 int
1172 linux_sched_getscheduler(struct thread *td,
1173     struct linux_sched_getscheduler_args *args)
1174 {
1175 	struct sched_getscheduler_args bsd;
1176 	int error;
1177 
1178 #ifdef DEBUG
1179 	if (ldebug(sched_getscheduler))
1180 		printf(ARGS(sched_getscheduler, "%d"), args->pid);
1181 #endif
1182 
1183 	bsd.pid = args->pid;
1184 	error = sched_getscheduler(td, &bsd);
1185 
1186 	switch (td->td_retval[0]) {
1187 	case SCHED_OTHER:
1188 		td->td_retval[0] = LINUX_SCHED_OTHER;
1189 		break;
1190 	case SCHED_FIFO:
1191 		td->td_retval[0] = LINUX_SCHED_FIFO;
1192 		break;
1193 	case SCHED_RR:
1194 		td->td_retval[0] = LINUX_SCHED_RR;
1195 		break;
1196 	}
1197 
1198 	return error;
1199 }
1200 
1201 int
1202 linux_sched_get_priority_max(struct thread *td,
1203     struct linux_sched_get_priority_max_args *args)
1204 {
1205 	struct sched_get_priority_max_args bsd;
1206 
1207 #ifdef DEBUG
1208 	if (ldebug(sched_get_priority_max))
1209 		printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1210 #endif
1211 
1212 	switch (args->policy) {
1213 	case LINUX_SCHED_OTHER:
1214 		bsd.policy = SCHED_OTHER;
1215 		break;
1216 	case LINUX_SCHED_FIFO:
1217 		bsd.policy = SCHED_FIFO;
1218 		break;
1219 	case LINUX_SCHED_RR:
1220 		bsd.policy = SCHED_RR;
1221 		break;
1222 	default:
1223 		return EINVAL;
1224 	}
1225 	return sched_get_priority_max(td, &bsd);
1226 }
1227 
1228 int
1229 linux_sched_get_priority_min(struct thread *td,
1230     struct linux_sched_get_priority_min_args *args)
1231 {
1232 	struct sched_get_priority_min_args bsd;
1233 
1234 #ifdef DEBUG
1235 	if (ldebug(sched_get_priority_min))
1236 		printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1237 #endif
1238 
1239 	switch (args->policy) {
1240 	case LINUX_SCHED_OTHER:
1241 		bsd.policy = SCHED_OTHER;
1242 		break;
1243 	case LINUX_SCHED_FIFO:
1244 		bsd.policy = SCHED_FIFO;
1245 		break;
1246 	case LINUX_SCHED_RR:
1247 		bsd.policy = SCHED_RR;
1248 		break;
1249 	default:
1250 		return EINVAL;
1251 	}
1252 	return sched_get_priority_min(td, &bsd);
1253 }
1254 
1255 #define REBOOT_CAD_ON	0x89abcdef
1256 #define REBOOT_CAD_OFF	0
1257 #define REBOOT_HALT	0xcdef0123
1258 
1259 int
1260 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1261 {
1262 	struct reboot_args bsd_args;
1263 
1264 #ifdef DEBUG
1265 	if (ldebug(reboot))
1266 		printf(ARGS(reboot, "0x%x"), args->cmd);
1267 #endif
1268 	if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF)
1269 		return (0);
1270 	bsd_args.opt = (args->cmd == REBOOT_HALT) ? RB_HALT : 0;
1271 	return (reboot(td, &bsd_args));
1272 }
1273 
1274 #ifndef __alpha__
1275 
1276 /*
1277  * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1278  * td->td_retval[1] when COMPAT_43 or COMPAT_SUNOS is defined. This
1279  * globbers registers that are assumed to be preserved. The following
1280  * lightweight syscalls fixes this. See also linux_getgid16() and
1281  * linux_getuid16() in linux_uid16.c.
1282  *
1283  * linux_getpid() - MP SAFE
1284  * linux_getgid() - MP SAFE
1285  * linux_getuid() - MP SAFE
1286  */
1287 
1288 int
1289 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1290 {
1291 
1292 	td->td_retval[0] = td->td_proc->p_pid;
1293 	return (0);
1294 }
1295 
1296 int
1297 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1298 {
1299 
1300 	td->td_retval[0] = td->td_ucred->cr_rgid;
1301 	return (0);
1302 }
1303 
1304 int
1305 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1306 {
1307 
1308 	td->td_retval[0] = td->td_ucred->cr_ruid;
1309 	return (0);
1310 }
1311 
1312 #endif /*!__alpha__*/
1313 
1314 int
1315 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1316 {
1317 	struct getsid_args bsd;
1318 	bsd.pid = args->pid;
1319 	return getsid(td, &bsd);
1320 }
1321